Home
21.
What is the output of the following code? (Select 1 option.)

class OpPre {
public static void main(String... args) {
int x = 10;
int y = 20;
int z = 30;
if (x+y%z > (x+(-y)*(-z))) {
System.out.println(x + y + z);
}
}
}
  • A.
    60
  • B.
    59
  • C.
    61
  • D.
    No Output
  • E.
    The code fails to compile.
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
x+y%z evaluates to 30; (x+(y%z)) and (x+(-y)*(-z)) evaluate to 610 . The if condition returns false and the line of code that prints the sum of x , y , and z doesn’t execute. Hence, the code doesn’t provide any output.
Report
Name Email  
22.
Select the most appropriate definition of the variable name and the line number on which it should be declared so that the following code compiles success-
fully (choose 1 option):
class EJava {
// LINE 1
public EJava() {
System.out.println(name);
}
void calc() {
// LINE 2
if (8 > 2) {
System.out.println(name);
}
}
public static void main(String... args) {
// LINE 3
System.out.println(name);
}
}
  • A.
    Define static String name ; on line 1.
  • B.
    Define String name; on line 1
  • C.
    Define String name; on line 2.
  • D.
    Define String name; on line 3.
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
The variable name must be accessible in the instance method calc , the class constructor, and the static method main . A non- static variable can’t be accessed by a static method. Hence, the only appropriate option is to define a static variable name that can be accessed by all—the constructor of class EJava , and methods calc and main .
Report
Name Email  
23.
Examine the following code and select the correct statement (choose 1 option):

line 1> class Emp {
line 2> Emp mgr = new Emp();
line 3> }
line 4>class Office {
line 5>public static void main(String args[]) {
line 6>Emp e = null;
line 7>e = new Emp();
line 8>e = null;
line 9>}
line 10> }
  • A.
    The object referred to by object e is eligible for garbage collection on line 8
  • B.
    The object referred to by object e is eligible for garbage collection on line 9.
  • C.
    The object referred to by object e isn’t eligible for garbage collection because its member variable mgr isn’t set to null .
  • D.
    The code throws a runtime exception and the code execution never reaches line 8 or 9.
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
The code throws java.lang.StackOverflowError at runtime. Line 7 creates an instance of class Emp . Creation of an object of the class Emp requires the cre- ation of an instance variable mgr and its initialization with an object of the same class. As you see, the Emp object creation calls itself recursively, resulting in java.lang .StackOverflowError .
Report
Name Email  
24.
Which of the following is the correct declaration of a method that accepts two String arguments and an int argument and doesn’t return any value? (Select 2 options.)
  • A.
    void myMethod(String str1, int str2, String str3)
  • B.
    myMethod(String val1, int val2, String val3)
  • C.
    void myMethod(String str1, str2, int a)
  • D.
    void myMethod(int str2, String str3, String str1)
  • Answer & Explanation
  • Report
Answer : [A, E]
Explanation :
The placement of the type of method parameters and the name of the method parameters doesn’t matter. You can accept two String variables and then an int variable or a String variable followed by int and again a String . The name of an int variable can be str2 . As long as the names are valid identifiers, any name is acceptable. The return type of the method should be void to specify that the method doesn’t return any value.
Option (b) is incorrect. It won’t compile because the method signature doesn’t include a return type.
Options (c) and (d) are incorrect. The method signatures of these methods don’t define data types for all their method parameters.
Report
Name Email  
25.
Which of the following will compile successfully? (Select 3 options.)
  • A.
    int eArr1[] = {10, 23, 10, 2};
  • B.
    int[] eArr2 = new int[10];
  • C.
    int[] eArr3 = new int[] {};
  • D.
    int[] eArr4 = new int[10] {};
  • E.
    int eArr5[] = new int[2] {10, 20};
  • Answer & Explanation
  • Report
Answer : [A, B, C]
Explanation :
Option (d) is incorrect because it defines the size of the array while using {} , which isn’t allowed. Both of the following lines of code are correct:
int[] eArr4 = new int[10];
int[] eArr4 = new int[]{};
Option (e) is incorrect because it’s invalid to specify the size of the array within the square brackets when you’re declaring, instantiating, and initializing an array in a single line of code.
Report
Name Email