Part A: 1 class B { 2 private String name; 3 public B(String s) { 4 name = s; 5
ID: 3714475 • Letter: P
Question
Part A:
1 class B {
2 private String name;
3 public B(String s) {
4 name = s;
5 }
6 public void finalize() {
7 System.out.print(name);
8 }
9 }
10
11 class E {
12 public static void m() {
13 B x1 = new B("X"), y1 = new B("Y");
14 }
15 public static void main(String[] args) {
16 m();
17 System.gc();
18 }
19 }
If the above is stored in a file called E.java, which 2 of the following could be a result of attempting to compile and run the program?
a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Nothing is printed.
e. None of the above
f. Compilation error
g. An exception is thrown
Part B:
class JJF1 {
public void main (String args[]) {
System.out.print(Byte.MIN_VALUE+",");
System.out.print(Byte.MAX_VALUE); } }
What is the result of attempting to compile and run the program?
a. Prints: 0,255
b. Prints: 0,256
c. Prints: -127,128
d. Prints: -128,127
e. Compile-time error
f. None of the above
Part C:
1 class A {
2 void m1(int i) {
3 int j = i % 3;
4 switch (j) {
5 case 0: System.out.print("0");
6 break;
7 case 1: System.out.print("1");
8 break;
9 default:
10 assert (j == 2);
11 System.out.print(j);
12 }
13 }
14 public static void main (String[] args) {
15 A a = new A();
16 for (int i=5; i >= -1; i--) {a.m1(i);}
17 }
18 }
Which statements are true? Choose 2.
a. With assertions enabled it prints 210210-1 followed by an AssertionError message.
b. With assertions enabled it prints 210210 followed by an AssertionError message.
c. With assertions enabled it prints only 210210.
d. With assertions enabled it prints nothing.
e. With assertions disabled it prints 210210-1
f. With assertions disabled it prints only 210210
g. Assertions should not be used within the default case of a switch statement.
Part D:
Which of these words belong to the set of Java keywords? Choose 2
a. transient
b. serializable
c. runnable
d. run
e. volatile
f. externalizable
g. cloneable
Part E:
class A {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) + m(i++) + m(-i) + m(i++));
}
}
What is the result of attempting to compile and run the above program? Choose 1.
a. Prints: 1, 2, 3, 4, 10,
b. Prints: 1, 2, -3, 4, 4,
c. Prints: 2, 2, -3, -3, -2,
d. Prints: 2, 2, -3, 3, 4,
e. Prints: 2, 3, -3, -2, 0,
f. Prints: 2, 3, -3, 4, 6,
g. Prints: 2, 3, 4, 5, 14,
h. Run-time error
i. Compile-time error
j. None of the above
Explanation / Answer
Hi,
The following are the solutions for each of the questions along with description. I hope, the answers help you.
Part A
//E.java
class B {
private String name;
public B(String s) {
name = s;
}
public void finalize() {
System.out.print(name);
}
}
class E {
public static void m() {
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args) {
m();
System.gc();
}
}
Solution: b
When the objects go out-of-scope, the JRE needs to free up the memory allocated to the objects i.e. x1 and y1. When the objects are being garbage-collected (GC), the finalize method is invoked.
In this case, in the finalize method, the value of the member variable is being printed. Typically, the default GC algorithm removes the youngest object first and hence, we see the "YX" being printed when the code is executed
Part B
//JJF1.java
class JJF1 {
public void main(String args[]) {
System.out.print(Byte.MIN_VALUE + ",");
System.out.print(Byte.MAX_VALUE);
}
}
Solution: f
When the above code is executed, the main method is NOT invoked as the method signature is incorrect. The JRE expects that the main method should be declared as "public static void main (String args[])" but in this case, "static" is missing and hence nothing gets printed on the screen.
Part C
//A.java
class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0");
break;
case 1: System.out.print("1");
break;
default:
assert (j == 2);
System.out.print(j);
}
}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}
}
Solution:
b - With assertions enabled it prints 210210 followed by an AssertionError message.
e - With assertions disabled it prints 210210-1
When the above code is executed with assertions enabled (java -ea A), then the following are the values of variables i and j
i, j, Result
5, 2 2
4, 1, 1
3, 0, 0
2, 2, 2
1, 1, 1
0, 0, 0,
-1, -1, (exception thrown when the assert statement is executed, as it expects the value of j to be 2)
Hence, with asserts enabled, the result is "b"
However, when asserts are disabled, the program continues to execute for all the values of i without any exceptions and prints 210210-1
Hence with asserts disabled, the result is "e"
Part D
a. transient
b. serializable
c. runnable
d. run
e. volatile
f. externalizable
g. cloneable
Solution: a, e
While transient and volatile are the keywords defined by the Java language, the rest are the classes or interfaces provided by the JDK library.
transient is used to avoid saving the value of a variable during serialization
volatile is used to ensure that any change in the value of the variable is reflected across all the threads that are using the variable
Part E
//A.java
class A {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) + m(i++) + m(-i) + m(i++));
}
}
Solution: d (2, 2, -3, 3, 4, )
The following is the order of execution.
First, the inner methods are invoked
m(++i) - due the preincrement operator, the value of i is now - 2
m(i++) - the value of i is still 2, hence 2 is again printed and after the method is invoked, due to the post increment operator the value of i is now 3
m(-i) - the value of i is 3, the method is invoked with value of -3, hence the printed value is -3
m(i++) - still, the value of i is 3, hence, the printed value is 3
Finally, the outermost method is invoked
Now, the return values for each of the methods are considered and the value of i is 4, and it gets printed on the screen.