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

class ColorPack {
int shadeCount = 12;
static int getShadeCount() {
return shadeCount;
}
}
class Artist {
public static void main(String args[]) {
ColorPack pack1 = new ColorPack();
System.out.println(pack1.getShadeCount());
}
}
  • A.
    10
  • B.
    12
  • C.
    No output
  • D.
    Compilation error
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
A static method can’t access non- static instance variables of a class. Hence, the class ColorPack fails to compile
Report
Name Email  
42.
Paul defined his Laptop and Workshop classes to upgrade his laptop’s mem- ory. Do you think he succeeded? What is the output of this code? (Select 1 option.)

class Laptop {
String memory = "1GB";
}
class Workshop {
public static void main(String args[]) {
Laptop life = new Laptop();
repair(life);
System.out.println(life.memory);
}
public static void repair(Laptop laptop) {
laptop.memory = "2GB";
}
}
  • A.
    1 GB
  • B.
    2 GB
  • C.
    Compilation error
  • D.
    Runtime exception
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
The method repair defined in this example modifies the state of the method parameter laptop that is passed to it. It does so by modifying the value of the instance variable memory .
When a method modifies the state of an object reference variable that is passed to it, the changes made are visible in the calling method. The method repair makes changes to the state of the method parameter laptop ; these changes are visible in the method main . Hence, the method main prints the value of life.memory as 2 GB .
Report
Name Email  
43.
What is the output of the following code? (Select 1 option.)
public class Application {
public static void main(String... args) {
double price = 10;
String model;
if (price > 10)
model = "Smartphone";
else if (price <= 10)
model = "landline";
System.out.println(model);
}
}
  • A.
    landline
  • B.
    smartphone
  • C.
    No output
  • D.
    Compilation Error
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
The local variables aren’t initialized with default values. Code that tries to print the value of an uninitialized local variable fails to compile.
In this code, the local variable model is only declared, not initialized. The initialization of the variable model is placed within the if and else-if constructs. If you initialize a variable within an if or else-if construct, the compiler can’t be sure whether these conditions will evaluate to true , resulting in no initialization of the local variable. Because there is no else at the bottom and the compiler can’t tell whether the if and else - if are mutually exclusive, the code won’t compile. If you remove the condition if (price <= 10) from the preceding code, the code will compile successfully:

public class Application {
public static void main(String... args) {
double price = 10;
String model;
if (price > 10)
model = "Smartphone";
else
model = "landline";
System.out.println(model);
}
}
In this code, the compiler can be sure about the initialization of the local variable model .
Report
Name Email  
44.
What is the output of the following code? (Select 1 option.)
class EString {
public static void main(String args[]) {
String eVal = "123456789";
System.out.println(eVal.substring(eVal.indexOf("2"),eVal.indexOf("0")).c
oncat("0"));
}
}
  • A.
    234567890
  • B.
    34567890
  • C.
    234456789
  • D.
    3456789
  • E.
    Compilation error
  • F.
    Runtime exception
  • Answer & Explanation
  • Report
Answer : [F]
Explanation :
When multiple methods are chained on a single code statement, the methods execute from left to right, not from right to left. eVal.indexOf("0") returns a negative value because, as you can see, the String eVal doesn’t contain the digit 0 . Hence, eVal.substring is passed a negative end value, which results in a RuntimeException .
Report
Name Email  
45.
Examine the following code and select the correct statements (choose 2 options):
class Artist {
Artist assistant;
}
class Studio {
public static void main(String... args) {
Artist a1 = new Artist();
Artist a2 = new Artist();
a2.assistant = a1;
a2 = null;
// Line 1
}
// Line 2
}
  • A.
    At least two objects are garbage collected on line 1.
  • B.
    At least one object is garbage collected on line 1.
  • C.
    No objects are garbage collected on line 1
  • D.
    The number of objects that are garbage collected on line 1 is unknown.
  • E.
    At least two objects are eligible for garbage collection on line 2.
  • Answer & Explanation
  • Report
Answer : [D, E]
Explanation :
Options (a), (b), and (c) are incorrect. When an object reference is marked as null , the object is marked for garbage collection. But you can’t be sure exactly when a garbage collector will kick in to garbage collect the objects. A garbage collector is a low-priority thread, and its exact execution time will depend on the OS. The OS will start this thread to claim unused space if it needs to claim unused space. You can be sure only about the number of objects that are eligible for garbage collection. You can never be sure about which objects have been garbage collected, so any statement that asserts that a particular number of objects have been garbage collected is incorrect.
Option (d) is correct. As mentioned previously, the exact number of objects gar- bage collected at any point in time can’t be determined.
Option (e) is correct. If you marked this option incorrect, think again. The ques- tion wants you to select the correct statements, and this is a correct statement. You may argue that at least two objects were already made eligible for garbage collection at line 1, and you are correct. But because nothing changes on line 2, at least two objects are still eligible for garbage collection.
Report
Name Email