Q1. Given:
class Cake {
int model;
String flavor;
Cake() {
model = 0;
flavor = "Unknown";
}
}
public class Test {
public static void main(String[] args) {
Cake c = new Cake();
bake1(c);
System.out.println(c.model + " " + c.flavor);
bake2(c);
System.out.println(c.model + " " + c.flavor);
}
public static Cake bake1(Cake c) {
c.flavor = "Strawberry";
c.model = 1200;
return c;
}
public static void bake2(Cake c) {
c.flavor = "Chocolate";
c.model = 1230;
return;
}
}
What is the result?
A. 0 unknown 0 unknown
B. 1200 Strawberry 1200 Strawberry
C. 1200 Strawberry 1230 Chocolate
D. Compilation fails
Answer: C
Explanation: 1200 Strawberry 1230 Chocolate
Q2. Which usage represents a valid way of compiling java source file with the name "Main"?
A. javac Main.java
B. java Main.class
C. java Main.java
D. javac Main
E. java Main
Answer: A
Explanation: The compiler is invoked by the javac command. When compiling a Java class, you must include the file name, which houses the main classes including the Java extension. So to run Main.java file we have to use command in option A. TO execute Java program we can use Java command but can't use it for compiling. https://docs.oracle.com/javase/tutorial/getStarted/application/index.html
Q3. Which two are valid array declaration?
A. Object array[];
B. Boolean array[3];
C. int[] array;
D. Float[2] array;
Answer: A,C
Q4. Given the code fragment:
// insert code here
arr[0] = new int[3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1] = new int[4];
arr[1][0] = 10;
arr[1][1] = 20;
arr[1][2] = 30;
arr[1][3] = 40;
Which two statements, when inserted independently at line // insert code here, enable the code to compile?
A. int [] [] arr = null;
B. int [] [] arr = new int [2];
C. int [] [] arr = new int [2] [ ];
D. int [] [] arr = new int [] [4];
E. int [] [] arr = new int [2] [0];
F. int [] [] arr = new int [0] [4];
Answer: C,E
Q5. Given the code fragment from three files:
Which code fragment, when inserted at line 2, enables the code to compile?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: E
Q6. Which two statements are true for a two-dimensional array?
A. It is implemented as an array of the specified element type.
B. Using a row by column convention, each row of a two-dimensional array must be of the same size.
C. At declaration time, the number of elements of the array in each dimension must be specified.
D. All methods of the class Object may be invoked on the two-dimensional array.
Answer: A,D
Q7. Given:
class Base {
// insert code here
}
public class Derived extends Base{
public static void main(String[] args) {
Derived obj = new Derived();
obj.setNum(3);
System.out.println("Square = " + obj.getNum() * obj.getNum());
}
}
Which two options, when inserted independently inside class Base, ensure that the class is being properly encapsulated and allow the program to execute and print the square of the number?
A. private int num; public int getNum() { return num; }public void setNum(int num) { this.num = num;}
B. public int num; protected public int getNum() { return num; }protected public void setNum(int num) { this.num = num;}
C. private int num;public int getNum() {return num;} private void setNum(int num) { this.num = num;}
D. protected int num; public int getNum() { return num; } public void setNum(int num) { this.num = num;}
E. protected int num; private int getNum() { return num; } public void setNum(int num) { this.num = num;}
Answer: A,D
Explanation:
Incorrect:
Not B: illegal combination of modifiers: protected and public
not C: setNum method cannot be private.
not E: getNum method cannot be private.
Q8. Given the code fragment:
Which two modifications, when made independently, enable the code to print joe:true: 100.0?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: A,C
Q9. Given the code fragment:
What is the result?
A. Execution terminates in the first catch statement, and caught a RuntimeException is printed to the console.
B. Execution terminates In the second catch statement, and caught an Exception is printed to the console.
C. A runtime error is thrown in the thread "main".
D. Execution completes normally, and Ready to us. is printed to the console.
E. The code fails to compile because a throws keyword is required.
Answer: C
Q10. Given the code fragment:
What is the result if the integer aVar is 9?
A. 10 Hello World!
B. Hello Universe!
C. Hello World!
D. Compilation fails.
Answer: A