import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
A -
This program will compile successfully.
B -
This program fails to compile due to an error at line 4.
C -
This program fails to compile due to an error at line 6.
D -
This program fails to compile due to an error at line 13.
2-
Which statement is true?
A -
Catch(X x) can catch subclasses of X where X is a subclass of Exception.
B -
The Error class is a RuntimeException.
C -
Any statement that can throw an Error must be enclosed in a try block.
D -
Any statement that can throw an Exception must be enclosed in a try block.
3-
Which is valid declaration of a float?
A -
float f = 1F;
B -
float f = 1.0;
C -
float f = "1";
D -
float f = 1.0d;
4-
What will be the output of the program?
import java.util.*;
class H
{
public static void main (String[] args)
{
Object x = new Vector().elements();
System.out.print((x instanceof Enumeration)+",");
System.out.print((x instanceof Iterator)+",");
System.out.print(x instanceof ListIterator);
}
}
A -
Prints: false,false,false
B -
Prints: false,false,true
C -
Prints: false,true,false
D -
Prints: true,false,false
5-
What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}
A -
one two three four
B -
four three two one
C -
four one three two
D -
one two three four one
6-
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?
A -
Foo.Bar b = new Foo.Bar();
B -
Foo.Bar b = f.new Bar();
C -
Bar b = new f.Bar();
D -
Bar b = f.new Bar();
7-
Which three guarantee that a thread will leave the running state?
yield()
wait()
notify()
notifyAll()
sleep(1000)
aLiveThread.join()
Thread.killThread()
A -
1, 2 and 4
B -
2, 5 and 6
C -
3, 4 and 7
D -
4, 5 and 7
8-
Which will contain the body of the thread?
A -
run();
B -
start();
C -
stop();
D -
main();
9-
What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) ? "assertion failed" : "assertion passed" ;
System.out.println("finished");
}
}
A -
finished
B -
Compiliation fails.
C -
An AssertionError is thrown and finished is output.
D -
An AssertionError is thrown with the message "assertion failed."
10-
What will be the output of the program?
public class BoolTest
{
public static void main(String [] args)
{
int result = 0;
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("tRuE");
Boolean b4 = new Boolean("false");
if (b1 == b2) /* Line 10 */
result = 1;
if (b1.equals(b2) ) /* Line 12 */
result = result + 10;
if (b2 == b4) /* Line 14 */
result = result + 100;
if (b2.equals(b4) ) /* Line 16 */
result = result + 1000;
if (b2.equals(b3) ) /* Line 18 */
result = result + 10000;
System.out.println("result = " + result);
}
}