Q1. Given the code fragment:
What is the result?
A. 2 4
B. 0 2 4 6
C. 0 2 4
D. Compilation fails
Answer: C
Q2. Given:
What is the result?
A. 100 210
B. Compilation fails due to an error in line n1
C. Compilation fails due to an error at line n2
D. Compilation fails due to an error at line n3
Answer: C
Q3. 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
Q4. Given the following array:
Which two code fragments, independently, print each element in this array?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
F. Option F
Answer: B,E
Explanation: All the remaining options have syntax errors
Q5. Which three statements are true about the structure of a Java class?
A. A class can have only one private constructor.
B. A method can have the same name as a field.
C. A class can have overloaded static methods.
D. A public class must have a main method.
E. The methods are mandatory components of a class.
F. The fields need not be initialized before use.
Answer: A,B,C
Explanation: A: Private constructors prevent a class from being explicitly instantiated by its
callers.
If the programmer does not provide a constructor for a class, then the system will always
provide a default, public no-argument constructor. To disable this default constructor,
simply add a private no-argument constructor to the class. This private constructor may be
empty.
B: The following works fine:
int cake() {
int cake=0;
return (1);
}
C: We can overload static method in Java. In terms of method overloading static method
are just like normal methods and in order to overload static method you need to provide
another static method with same name but different method signature.
Incorrect:
Not D: Only a public class in an application need to have a main method.
Not E:
Example:
class A
{
public string something;
public int a;
}
Q: What do you call classes without methods? Most of the time: An anti pattern.
Why? Because it faciliates procedural programming with "Operator" classes and data
structures. You separate data and behaviour which isn't exactly good OOP.
Often times: A DTO (Data Transfer Object)
Read only datastructures meant to exchange data, derived from a business/domain object.
Sometimes: Just data structure.
Well sometimes, you just gotta have those structures to hold data that is just plain and
simple and has no operations on it.
Not F: Fields need to be initialtized. If not the code will not compile.
Example:
Uncompilable source code - variable x might not have been initialized
Q6. Given:
Which code fragment, when inserted at line 7, enables the code print true?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q7. Given:
public class X {
static int i;
int j;
public static void main(String[] args) {
X x1 = new X();
X x2 = new X();
x1.i = 3;
x1.j = 4;
x2.i = 5;
x2.j = 6;
System.out.println(
x1.i + " "+
x1.j + " "+
x2.i + " "+
x2.j);
}
}
What is the result?
A. 3 4 5 6
B. 3 4 3 6
C. 5 4 5 6
D. 3 6 4 6
Answer: C
Q8. Which two statements correctly describe checked exception?
A. These are exceptional conditions that a well-written application should anticipate and recover from.
B. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
C. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
D. Every class that is a subclass of RuntimeException and Error is categorized as checked exception.
E. Every class that is a subclass of Exception, excluding RuntimeException and its subclasses, is categorized as checked exception.
Answer: B,D
Explanation: Checked exceptions:
* (B) represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
* are subclasses of Exception It's somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).
* a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)
Reference: Checked versus unchecked exceptions
Q9. Given the code fragment:
What is the result if the integer aVar is 9?
A. 10 Hello world!
B. 10 Hello universe!
C. 9 Hello world!
D. Compilation fails.
Answer: A
Q10. Given:
And the commands:
Javac Jump.java
Java Jump crazy elephant is always
What is the result?
A. Lazy lion is jumping
B. Lion is always jumping
C. Crazy elephant is jumping
D. Elephant is always jumping
E. Compilation fails
Answer: B
Q11. Given:
What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: C
Q12. Which of the following can fill in the blank in this code to make it compile?
A. abstract
B. public
C. default
D. It will not compile with any as interfaces cannot have non abstract methods.
E. It will compile without filling the blank.
Answer: C
Explanation:
From Java SE 8, we can use static and/or default methods in interfaces, but they should be non abstract methods. SO in this case using default in blank is completely legal. Hence option C is correct. Option A is incorrect as given method is not abstract, so can't use abstract there. Options B and E are incorrect as we can't have non abstract method interface if they are not default or static. https;//docs.oraclexom/javase/tutorial/java/Iandl/defaultmethods.html
Q13. Given:
What is the result?
A. 1
1
1
B. 1
2
3
C. 2
3
4
D. Compilation fails
E. The loop executes infinite times
Answer: E
Q14. Given:
What is the result?
A. 10 : 22 : 20
B. 10 : 22 : 22
C. 10 : 22 : 6
D. 10 : 30 : 6
Answer: B
Q15. Given the code fragment:
String[] cartoons = {"tom","jerry","micky","tom"};
int counter =0;
if ("tom".equals(cartoons[0])) {
counter++;
} else if ("tom".equals(cartoons[1])) {
counter++;
} else if ("tom".equals(cartoons[2])) {
counter++;
} else if ("tom".equals(cartoons[3])) {
counter++;
}
System.out.print(counter);
What is the result?
A. 1
B. 2
C. 4
D. 0
Answer: A
Explanation: Counter++ will be executed only once because of the else if constructs.