You are on page 1of 5

JAVA TEST 2

1] Which code declares class A to belong to the mypackage.financial package?


a) package mypackage; package financial;
b) import mypackage.*
c) package mypackage.financial.a;
d) import mypackage.financial.*; package mypackage.financial;

2) How can you have a "try" block that invokes methods that throw two different exceptions?
a) Catch one exception in a "catch" block and the other in a "finally" block.
b) Setup nested "catch" blocks for each exception.
c) Catch one exception in a "catch" block and the other via the return value.
d) d)Use wait() between the calls to process all exceptions before continuing.
e) e)Include a "catch" block for each exception.

3] Which one of the following describes the difference between StringBuffer and String?
a) StringBuffer is used only to buffer data from an input or output stream.
b) b)StringBuffer allows text to be changed after instantiation.
c) c)StringBuffer holds zero length Strings.
d) StringBuffer supports Unicode.
e) StringBuffer is an array of Strings.

4 Which of the following methods belongs to Thread class:


(A) wait() (B) notify() (C) start() (D) sleep() (E) join()
a) (A) and (B)
b) (C) and (D)
c) (E) and (A)
d) (C) and (D) and (A)

5) abstract’ modifier can be applied to :


a) classes, methods and variables b) methods and variables
c) classes and methods d) classes and variables

6) Select the correct command to compile package:


a) javac –d . <filename>.java B .) javac <filename>.java
C. java <filename> D) javac <classname>.java
7) Select correct statements from the following:
a) A class can extend another class and implement an interface at a time.
b) An interface can extend another interface.
c) An interface can extend another class.
d) An interface implement another interface.

8) While overriding the method, its access modifier can be changed from: (Select correct option)
a) public to private b) protected to public c) public to default d) default to private

9) Select the correct sequence of the access modifiers from most restrictive to less resteictive:
a) public,protected,default,private b) public,default,protected,private
c) private,default,protected,public d) private, protected,default,public

10) Thread is alive in NEW state. State True or False.


a) a)True b)False
11). Which of these classes have a one-parameter constructor taking a string? Select 2
a) Void b ) Integer c) Boolean d) Character e) Object

12) Which statements are true? Select 2


a) If an exception is uncaught in a method, the method will terminate and normal execution will resume.
b) An overriding method must declare that it throws the same exception classes as the method it overrides.
c) The main() method of a program can declare that it throws checked exceptions.
d) A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.
e) finally blocks are executed if, and only if, an exception gets thrown while inside the corresponding try block.

13).What is wrong with the following code?


public class MyClass {
public static void main(String[] args) throws A {
try {
f();
} finally {
System.out.println("Done.");
} catch (A e) {
throw e;
}
}

public static void f() throws B {


throw new B();
}
}
class A extends Throwable {}

class B extends A {}
Select the one correct answer.
a) The main() method must declare that it throws B.
b) The finally block must follow the catch block in the main() method.
c) The catch block in the main() method must declare that it catches B rather than A.
d) A single try block cannot be followed by both a finally and a catch block.
e) The declaration of class A is illegal.

14). Given:
public class Messager implements Runnable {
public static void main(String[] args) {
new Thread(new Messager("Wallace")).start() ;
new Thread(new Messager("Gromit")).start();
}
private String name;
public Messager(String name) { this.name = name; }
public void run() {
message(1);
message(2);
}
private synchronized void message(int n)
{
System.out.print(name + "-" + n + " ");
}
}
Which of the following is a possible result? (Choose all that apply.)
a) Wallace-1 Wallace-2 Gromit-1
b) Wallace-1 Gromit-2 Wallace-2 Gromit-1
c) Wallace-1 Gromit-1 Gromit-2 Wallace-2
d) Gromit-1 Gromit-2
e) Gromit-2 Wallace-1 Gromit-1 Wallace-2
f) The code does not compile.
g) An error occurs at run time.

15) . The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
. Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
a) public class MyRunnable extends Runnable{public void run(){}}
b) public class MyRunnable extends Object{public void run(){}}
c) public class MyRunnable implements Runnable{public void run(){}}
d) public class MyRunnable implements Runnable{void run(){}}
e) public class MyRunnable implements Runnable{public void start(){}}

16) Given:
class Emu {
static String s = "-";
public static void main(String[] args) {
try {
throw new Exception();
} catch (Exception e) {
try {
try { throw new Exception();
} catch (Exception ex) { s += "ic "; }
throw new Exception(); }
catch (Exception x) { s += "mc "; }
finally { s += "mf "; }
} finally { s += "of "; }
System.out.println(s);
}}
What is the result?
a) -ic of
b) -mf of
c) -mc mf
d) -ic mf of
e) -ic mc mf of
f) -ic mc of mf
g) Compilation fails.

17) Which of the following collections can you use to store key-value pairs and is thread safe?
a) HashTabl b) HashMa c) TreeMap d) Vector
18). Given:
public static void before () {
Set set = new TreeSet();
set.add("2");
set.add(3);
set.add("1");
Iterator it = set.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}Which of the following statements are true?

a) The before() method will print 1 2


b) The before() method will print 1 2 3
c) The before() method will print three numbers, but the order cannot be determined.
d) The before() method will not: compile.
e) The before() method will throw an exception at runtime.

19).which of the following classes implements Comparable


a) String b) StringBuffer c) File d) Int e) None of the above
20) Given the following program, which lines will print 11 exactly?
class MyClass {
public static void main(String[] args) {
double v = 10.5;
System.out.println(Math.ceil(v)); // (1)
System.out.println(Math.round(v)); // (2)
System.out.println(Math.floor(v)); // (3)
System.out.println((int) Math.ceil(v)); // (4)
System.out.println((int) Math.floor(v)); // (5)
}
}
Select the two correct answers.
A)The line labeled (1). B) The line labeled (2). C) The line labeled (3).
D) The line labeled (4). E) The line labeled (5)

21. . Which of these classes have a one-parameter constructor taking a string? Select the two
A) Void B) Integer C) Boolean D) Character E) Object

22.. What is the name of the interface used to represent collections that maintain non-unique elements in order? Select the one
correct answer.
a. Collection B) Set C) SortedSet D) List E) Sequence

23. Given:
public static void before () {
Set set = new TreeSet();
set.add("2");
set.add(3);
set.add("1");
Iterator it = set.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}
Which of the following statements are true?
A. The before() method will print 1 2
B. The before() method will print 1 2 3
C. The before() method will print three numbers, but the order cannot be determined.
D. The before() method will not: compile.
E. The before() method will throw an exception at runtime.

24. Given:
class Swill {
public static void main(String[] args) {
String s = "-";
switch(TimeZone.CST) {
case EST: s += "e";
case CST: s += "c";
case MST: s += "m";
default: s += "X";
case PST: s += "p";
}
System.out.println(s);
}
}
enum TimeZone {EST, CST, MST, PST }
What is the result?
A. -c
B. -X
C. -cm
D. -cmp
E. -cmXp
F. Compilation fails.

You might also like