Q1. Given:
public class TestOperator {
public static void main(String[] args) {
int result = 30 -12 / (2*5)+1;
System.out.print("Result = " + result);
}
}
What is the result?
A. Result = 2
B. Result = 3
C. Result = 28
D. Result = 29
E. Result = 30
Answer: E
Q2. 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
Q3. 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
Q4. Given:
public class MyClass {
public static void main(String[] args) {
while (int ii = 0; ii < 2) {
ii++;
System.out.println("ii = " + ii);
}
}
}
What is the result?
A. ii = 1 ii = 2
B. Compilation fails
C. The program prints nothing
D. The program goes into an infinite loop with no output
E. The program goes to an infinite loop outputting: ii = 1 ii = 1
Answer: B
Explanation: The while statement is incorrect. It has the syntax of a for statement.
The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
while (expression) { statement(s) } The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
Reference: The while and do-while Statements
Q5. Given the definitions of the MyString class and the Test class:
What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: C
Q6. Given:
public class TestLoop {
public static void main(String[] args) {
int array[] = {0, 1, 2, 3, 4};
int key = 3;
for (int pos = 0; pos < array.length; ++pos) {
if (array[pos] == key) {
break;
}
}
System.out.print("Found " + key + "at " + pos);
}
}
What is the result?
A. Found 3 at 2
B. Found 3 at 3
C. Compilation fails
D. An exception is thrown at runtime
Answer: C
Explanation: The following line does not compile: System.out.print("Found " + key + "at " + pos);
The variable pos is undefined at this line, as its scope is only valid in the for loop. Any variables created inside of a loop are LOCAL TO THE LOOP.
Q7. Which of the following exception will be thrown due to the statement given here?
int array[] = new int[-2];
A. NullPointerException
B. NegativeArraySizeException
C. ArrayIndexOutOfBoundsException
D. IndexOutOfBoundsException
E. This statement does not cause any exception.
Answer: B
Explanation:
In given statement we can see that, we have passed negative value for creating int array,
which results a NegativeArraySize Except ion. Hence option B is correct.
Option A is incorrect as it is thrown when an application attempts to use null in a case
where an object is required.
Option D is incorrect as IndexOutOfBoundsException thrown to indicate that an index of
some sort (such as to an array, to a string, or to a vector) is out of range.
REFERENCE
rhttpy/docs.oracle.com/iavase/S/docs/api/java/lang/NegativeArraySizeException.html
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. Which two actions will improve the encapsulation of a class?
A. Changing the access modifier of a field from public to private
B. Removing the public modifier from a class declaration
C. Changing the return type of a method to void
D. Returning a copy of the contents of an array or ArrayList instead of a direct reference
Answer: A,D
Reference: http://www.tutorialspoint.com/java/java_access_modifiers.htm
Q10. Given the code fragment:
Which statement is true?
A. After line 8, three objects are eligible for garbage collection
B. After line 8, two objects are eligible for garbage collection
C. After line 8, one object is eligible for garbage collection
D. After line 8, none of the objects are eligible for garbage collection
Answer: C
Q11. Given the fragment:
What is the result?
A. 13480.0
B. 13480.02
C. Compilation fails
D. An exception is thrown at runtime
Answer: A
Q12. Given the following code:
What is the output?
A. 4
4
B. 3
5
C. 4
7
D. 5
4
E. 4
5
F. 4
21
Answer: E
Q13. Given the code fragment:
Which code fragment, when inserted at line n1, enables the App class to print Equal?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: B
Q14. Given:
Which two classes use the shape class correctly?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
F. Option F
Answer: B,E
Explanation: When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class (E). However, if it does not, then the subclass must also be declared abstract (B). Note: An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
Q15. Given:
What is the result?
A. Initialized Started
B. Initialized Started Initialized
C. Compilation fails
D. An exception is thrown at runtime
Answer: B