Home
21.
What is the result of compiling and executing the following class? 1: public class Tolls { 2: private static int yesterday = 1; 3: int tomorrow = 10; 4: public static void main(String[] args) { 5: Tolls tolls = new Tolls(); 6: int today=20, tomorrow = 40; 7: System.out.print(today + tolls.tomorrow + tolls.yesterday); 8: } 9: }
  • A.
    The code does not compile due to line 6.
  • B.
    The code does not compile due to line 7.
  • C.
    31
  • D.
    61
  • Answer & Explanation
  • Report
Answer : [C]
Explanation :
The code compiles and runs without issue, so Options A and B are incorrect. The question relies on your ability to understand variable scope. The variable today has local scope to the method in which it is executed. The variable tomorrow is re-declared in the method, but the reference used on line 7 is to the instance variable with a value of 10. Finally, the variable tomorrow is static. While using an instance reference to access a static variable is not recommended, it does not prevent the variable from being read. The result is line 7 evaluates and prints (20 + 10 + 1) = 31, making C the correct answer.
Report
Name Email  
22.
Given the following class definition, which is the only line that does not contain a compilation error?
1: public ThisClassDoesNotCompile {
2: double int count;
3: void errors() {}
4: static void private limit; }
  • A.
    Line 1.
  • B.
    Line 2
  • C.
    line 3.
  • D.
    line 4 .
  • Answer & Explanation
  • Report
Answer : [C]
Explanation :
Line 1 is missing the class keyword. Line 2 contains two types for the same variable. Line 3 is a valid definition for a method, making C the correct answer. Finally, line 4 contains an access modifier, private, after the return type, which is not allowed. In addition, void is an invalid type for variables.
Report
Name Email  
23.
Which of the following features allows a Java class to be run on a wide variety of computers and devices?
  • A.
    Encapsulation
  • B.
    Object oriented
  • C.
    Inheritance
  • D.
    Platform independence
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
Platform independence is the property of Java that allows it to be run on a variety of different devices.
Report
Name Email  
24.
Which of the following is not a property of a JVM?
  • A.
    It prevents Java bytecode from being easily decoded/decompiled.
  • B.
    It supports platform independence.
  • C.
    It manages memory for the application.
  • D.
    It translates Java instructions to machine instructions.
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
Options B, C, and D are each correct statements about JVMs. Option A is incorrect. Not only is it not a statement about JVMs, it is actually false as Java bytecode can often be easily decoded/decompiled.
Report
Name Email  
25.
Which of the following variables are always in scope for the entire program?
  • A.
    Package variables
  • B.
    Class variables
  • C.
    Instance variables
  • D.
    Local variables
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
There is no such thing as package variables, so Option A is incorrect. Option C is incorrect as the variable is only in scope within a specific instance of the class. Option D is also incorrect as the variable is only in scope for a single method that it is defined in. Option B is the only correct answer as class variables are in scope within the program.
Report
Name Email