Q1. Given:
What is the result?
A. 2 4 6 8 10 12
B. 2 4 6 8 10 12 14
C. Compilation fails
D. The program prints multiple of 2 infinite times
E. The program prints nothing
Answer: B
Q2. Which three statements are benefits of encapsulation?
A. Allows a class implementation to change without changing t he clients
B. Protects confidential data from leaking out of the objects
C. Prevents code from causing exceptions
D. Enables the class implementation to protect its invariants
E. Permits classes to be combined into the same package
F. Enables multiple instances of the same class to be created safely
Answer: A,B,D
Q3. 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
Q4. Given the code fragment:
float x = 22.00f % 3.00f;
int y = 22 % 3;
System.out.print(x + ", "+ y);
What is the result?
A. 1.0, 1
B. 1.0f, 1
C. 7.33, 7
D. Compilation fails
E. An exception is thrown at runtime
Answer: A
Q5. Given the code fragment:
public class Test {
static String[][] arr =new String[3][];
private static void doPrint() {
//insert code here
}
public static void main(String[] args) {
String[] class1 = {"A","B","C"};
String[] class2 = {"L","M","N","O"};
String[] class3 = {"I","J"};
arr[0] = class1;
arr[1] = class2;
arr[2] = class3;
Test.doPrint();
}
}
Which code fragment, when inserted at line //insert code here, enables the code to print
COJ?
A. int i = 0;
for (String[] sub: arr) {
int j = sub.length -1;
for (String str: sub) {
System.out.println(str[j]);
i++;
}
}
B. private static void doPrint() {
for (int i = 0;i < arr.length;i++) {
int j = arr[i].length-1;
System.out.print(arr[i][j]);
}
}
C. int i = 0;
for (String[] sub: arr[][]) {
int j = sub.length;
System.out.print(arr[i][j]);
i++;
}
D. for (int i = 0;i < arr.length-1;i++) {
int j = arr[i].length-1;
System.out.print(arr[i][j]);
i++;
}
Answer: B
Explanation:
Incorrect:
not A: The following line causes a compile error:
System.out.println(str[j]);
Not C: Compile erro line:
for (String[] sub: arr[][])
not D: Output: C
Q6. Which code fragment cause a compilation error?
A. flat flt = 100F;
B. float flt = (float) 1_11.00;
C. float flt = 100;
D. double y1 = 203.22; floatflt = y1
E. int y2 = 100;
floatflt = (float) y2;
Answer: B
Q7. Given:
A. Ym Xm2
B. Ym Xm1
C. Compilation fails
D. A ClassCastException is thrown at runtime
Answer: B
Q8. 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.
Q9. Given:
How many MarkList instances are created in memory at runtime?
A. 1
B. 2
C. 3
D. 4
Answer: A
Q10. Given:
interface Pet { }
class Dog implements Pet { }
public class Beagle extends Dog{ }
Which three are valid?
A. Pet a = new Dog();
B. Pet b = new Pet();
C. Dog f = new Pet();
D. Dog d = new Beagle();
E. Pet e = new Beagle();
F. Beagle c = new Dog();
Answer: A,D,E
Explanation:
Incorrect:
Not B, not C: Pet is abstact, cannot be instantiated.
Not F: incompatible type. Required Beagle, found Dog.
Q11. Consider following interface.
Which of the following will create instance of Runnable type?
A. Runnable run = 0 -> {System.out.println("Run");}
B. Runnable run = 0 -> System.outprintlnfRun");
C. Runnable run = 0 > System.outprintlnfRun");
D. Runnable run = > System.ouLprintlnfRun"};
E. None of the above.
Answer: A
Explanation:
Option A is the correct answer.
To create we have used following method with LocalDate class;
public static LocalDate of(intyear, int month, intdayOfMonth)
Here we need to remember that month is not zero based so if you pass 1 for month, then
month will be January.
Then we have used period object of 1 day and add to date object which makes current date
to next day, so final output is 2015-03-27. Hence option A is correct.
Q12. Which statement is true about Java byte code?
A. It can run on any platform.
B. It can run on any platform only if it was compiled for that platform.
C. It can run on any platform that has the Java Runtime Environment.
D. It can run on any platform that has a Java compiler.
E. It can run on any platform only if that platform has both the Java Runtime Environment and a Java compiler.
Answer: C
Q13. Which three statements describe the object-oriented features of the Java language?
A. Objects cannot be reused.
B. A subclass can inherit from a superclass.
C. Objects can share behaviors with other objects.
D. A package must contain more than one class.
E. Object is the root class of all other objects.
F. A main method must be declared in every class.
Answer: B,C,E
Q14. Given the code fragment:
public static void main(String[] args) {
int iArray[] = {65, 68, 69};
iArray[2] = iArray[0];
iArray[0] = iArray[1];
iArray[1] = iArray[2];
for (int element : iArray) {
System.out.print(element + " ");
}
A. 68, 65, 69
B. 68, 65, 65
C. 65, 68, 65
D. 65, 68, 69
E. Compilation fails
Answer: B
Q15. Which of the following will print current time?
A. System.out.print(new LocalTime()-now0);
B. System.out.print(new LocalTime());
C. System.ouLprint(LocalTime.now());
D. System.ouLprint(LocalTime.today());
E. None of the above.
Answer: C
Explanation:
The LocalTime is an interface, so we can't use new keyword with them. So options A and C are incorrect. To get current time we can call now method on LocalTime interface. So option C is correct. Option D is incorrect as there is no method called today as in LocalTime interface
https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html