1. For the following code :
import java.io.*;
public class Test {
public static void main (String [] args) {
try {
File f = new File("file.txt");
OutputStream out = new FileOutputStream(f, true);
}
catch(IOException e) {
}
}
}
a) The code does not compile b) The code runs and no change is made to the file c) The code runs and sets the length of the file to 0 d) An exception is thrown because the file is not closed
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) The code runs and no change is made to the file.
Correct! The code runs and no change is made to the file.
2. What is the output?
int i = 1;
int[] f = new int[3];
int b = f[i];
int z = b + i;
System.out.println(z);
a) 0 b) 2 c) Compile error d) 1
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) 1. int array is assigned 0 by default
Correct! d) 1. int array is assigned 0 by default
3. What is the output?
public class Test {
public static void main (String [] args) {
int i=1, j=10;
do {
if(i++ > --j) continue;
}
while(i<5);
System.out.println(i+", "+j);
}
}
a) 5, 5 b) 6, 6 c) 5, 6 d) 6, 5
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) 5, 6 i > j is checked before incrementing
Correct! 5, 6 i > j is checked before incrementing
4. What is the output?
public class Test {
public static String out ="";
public static void chk(int i) {
try {
if(i==1) {
throw new Exception();
}
out += "A";
}
catch(Exception e) {
out += "D";
return;
}
finally {
out += "B";
}
out += "C";
}
public static void main (String [] args) {
chk(0);
chk(1);
System.out.println(out);
}
}
a) DBCAB b) ADBCDB c) ABCD d) ABCDB
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) ABCDB
Correct! ABCDB
5. Which statement is true?
a) If only one thread is blocked in the wait method of an object, and another thread executes
the notify on the same object, then the first thread immediately resumes execution.
b) If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
c) If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct
and sole consequence of the notify call.
d) If two threads are blocked in the wait method of one object,and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong! The correct answer is b) If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
Correct! If a thread is blocked in the wait method of an object, and another thread executes the
notify method on the same object, it is still possible that the first thread might never resume execution.
6. Under which conditions will check() return true when called from a different class?
private int x, y;
private synchronized void setX(int i) {
x = i;
}
private synchronized void setY(int i) {
y = i;
}
public void setXY(int i) {
setX(i); setY(i);
}
public synchronized boolean check() {
return x!=y;
}
a) check() can return true when setXY is called by multiple threads. b) check() can never return true. c) check() can return true when multiple threads call setX and setY separately. d) check() can only return true if code is changed to allow x and y to be set separately.
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) check() can return true when setXY is called by multiple threads.
Correct! check() can return true when setXY is called by multiple threads.
7. What is output?
public class Test {
public static void main(String args[]) {
int a=1, b=2, c=3;
a=b=c;
System.out.println(a+" "+b+" "+c);
}
}