Friday, June 23, 2017

Problem Solving - Classes, Iterations and Arrays,String, Enum mcqs

1. What is stored in the object obj in the following lines of code? Box obj;
Ans: NULL

2. Which is not an Access Modifier?
Ans: Final

3. Which access modifiers in java is accessed by other classes in the same package or any subclasses of same package or different package?
Ans: Protected

4. Arrange the access levels in order of decreasing accessibility
Ans: Public Default Private

5. What is not true about default constructor. Choose all that apply.
Ans: Any constructor which is always used to create an object can be called default constructor. It may or may not have parameters.

6. All the user defined classes in java implicitly have a default constructor even if the constructor is not defined explicitly
Ans: True

7. Which is not Non-Access Modifiers?
Ans: Private

8.______is a default package in Java
Ans: java.lang

9. Which of the following statement is true about static methods
Ans: All of the above (A static method belongs to the class rather than object of a class, A static method can be invoked without the need for creating an instance of a class, A static method can access static data member and can change the value of it)

10. What will be the value of after execution of the following statements:
int a-10, b-14;
a-((a>b)?(b-a):(b+a))
Ans: 24.0

**11. There can be more than one java class in same file if.(Choose all that apply).
Ans: Only 1 class has public access modifier and has the same name as the .java file

12. What is the output of the following program
public class demo{
public static void main(String[] args){
System.out.println("hi" +args.length);
}
}
Ans: hi 0

13. What if I write static public void instead of public static void while defining main method?
Ans: Program compiles and runs successfully

14. public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()),i);
}
}
Ans: Compiler error complaining about forward referencing

15. The static can be used for?
Ans: All the above (variable(also known as class variable), method(also known as class method), nested class)

**16. Can you say that state of an object is similar to attribute of the class?
Ans: True

17. Which keyword can be used to refer current class instance variable?
Ans: this

18._______is the ability to define methods for derived classes
Ans: Polymorphism

19. Constructors can be private
Ans: True

20. How many constructors can be there in a class?
Ans: any number

21. Arrange the access levels in order of decreasing accessibilty
Ans: public default private

22. Which keyword in Java declares a variable whose value cannot change?
Ans: final

23. What is the output of the below program?
class Number
{
public static void main(String[] args)
{
int num=50;
switch(num>=60)
{
case true: System.out.println("Number lesser than 60!");
break;
case false: System.out.println("Number greater than 60!");
break;
default: System.out.println("Default Case");
break;
}
Ans: Number greater than 60!

**24. Importing a package imports the subpackages as well?
Ans: false

25. A constructor
Ans: A, B and C ( a) must have the same name as the class it is declared, b) is used to create objects, c) may be declared private)

**26. public class Exam1
{ public static void main(String args[])
{ private int a=0;
System.out.println(a++);
}}
What is the output of the above program?
Ans: Compile time error

27.Which methods can access to private attributes of a class?
Ans: Only methods those defined in the same class

28. What is the output of the following program?
public class Employee{
public String firstName;
public String lastName;
public int age;
public char gender;

public Employee(String firstNameForThisObject,String lastNameForThisObject;char gender){
//TODO Auto-generated constructor stub
firstName=firstNameForThisObject;
lastName=lastNameForThisObject;
gender=gender;
}
public static void main(String[] args){
//TODO Auto-generated method stub
Employee employee=new Employee("firstNameForThisObject","lastNameForThisObject",M);
System.out.println("first name is:"+employee.firstName);
System.out.println("last name is:"+employee.lastName);
System.out.println("age is:"+employee.age);
System.out.println("gender is:"+employee.gender);
}
}
Ans: compilation error

29. What is the output of the below mentioned code
public class Sample
{
public static void main(String[] args)
{
Demo d=new Demo(20);
System.out.println(d,num);
}
class Demo
{
final int num;
public Demo(int n)
{
num=n;
}
}
Ans: 20

30. public class Test1{
static{
System.out.println("Static block");
}
public static void main(String args[]){
System.out.println("Main method");
}
public static void staticMethod(){
System.out.println("Static method");
}
}
What will be the output?
Ans: Static block Main method

31. What is the output of the following program?
class MyClass
{
public static void main(String[] args)
{
int x;
System.out.println("Begin Main");
x=test();
System.out.println("End Main");
}
static void test()
{
System.out.println("Inside Test()");
return 0;
}
}
Ans: Compile time error

32. What if I do not provide the String array as the argument to the main method?
Ans: Run-time error

33. Which of the below access specifiers can be used with keyword class?
Ans: public

34. What will the below mentioned program print?

public class Sample
{
public static void main(String args[])
{
boolean b=false;
if(b=true)
System.out.println("True");
else
System.out.println("False");
}
}
Ans: True

2 comments: