Q31. Given:
What is the result?
A. Marrown
String out of limits
JesOran
B. Marrown
String out of limits
Array out of limits
C. Marrown
String out of limits
D. Marrown
NanRed
JesOran
Answer: A
Q32. Given the code fragment:
Which option can replace xxx to enable the code to print 135?
A. int e = 0; e < = 4; e++
B. int e = 0; e < 5; e + = 2
C. int e = 1; e < = 5; e + = 1
D. int e = 1; e < 5; e+ =2
Answer: B
Q33. Given:
public class ComputeSum {
public int x;
public int y;
public int sum;
public ComputeSum (int nx, int ny) {
x = nx; y =ny;
updateSum();
}
public void setX(int nx) { x = nx; updateSum();}
public void setY(int ny) { x = ny; updateSum();}
void updateSum() { sum = x + y;}
}
This class needs to protect an invariant on the sum field.
Which three members must have the private access modifier to ensure that this invariant is maintained?
A. The x field
B. The y field
C. The sum field
D. The ComputerSum ( ) constructor
E. The setX ( ) method
F. The setY ( ) method
Answer: C,E,F
Explanation: The sum field and the two methods (setX and SetY) that updates the sum field.
Q34. Given the code fragment:
Which two modifications should you make so that the code compiles successfully?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: A,C
Explanation:
Add throws clause in both printFileContent and main.
Q35. Which statement is/are true?
I. Default constructor only contains "super();" call.
II. We can't use any access modifier with a constructor.
III. A constructor should not have a return type.
A. Only I.
B. Only II.
C. Only I and II.
D. Only I and III.
E. AIL
Answer: D
Explanation:
Statement I is correct as the default constructor only contains super0 call
Statement II is incorrect as we can use any access modifier with a constructor.
Statement III is correct as constructor can't have return type, even void.
So option D is correct.
httpsy/docs.oracle.com/javase/tutorial/iava/javaOO/construaors.html
Q36. Given:
And the commands:
Javac Test.java
Java Test 12345
What is the result?
A. Number us : 12345
B. A NullPointerException is thrown at runtime
C. A NumberFormatException is thrown at runtime
D. AnArrayIndexOutOfBoundException is thrown at runtime.
Answer: A
Q37. Given:
A. a, e
i, o
B. a, e
o, o
C. e, e
I, o
D. e, e
o, o
Answer: B
Q38. Given:
public class Test {
public static void main(String[] args) {
int ax = 10, az = 30;
int aw = 1, ay = 1;
try {
aw = ax % 2;
ay = az / aw;
} catch (ArithmeticException e1) {
System.out.println("Invalid Divisor");
} catch (Exception e2) {
aw = 1;
System.out.println("Divisor Changed");
}
ay = az /aw; // Line 14
System.out.println("Succesful Division " + ay);
}
}
What is the result?
A. Invalid Divisor
Divisor Changed
Successful Division 30
B. Invalid Divisor
Successful Division 30
C. Invalid Divisor
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Teagle.main(Teagle.java:14)
D. Invalid Divisor
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Teagle.main(Teagle.java:14)
Successful Division 1
Answer: C
Q39. 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
Q40. 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.