Q. 1 ]Which colour is used to indicate instance methods in the standard "javadoc" format documentation:
A. blue
B. red
C. purple
D. orange
Q. 2 ]What is the correct ordering for the import, class and package declarations when found in a single file?
A. package, import, class
B. class, import, package
C. import, package, class
D. package, class, import
Q. 3 ]Which methods can be legally applied to a string object?
A. equals(String)
B. equals(Object)
C. trim()
D. round()
E. toString()
Q. 4 ]What is the parameter specification for the public static void main method?
A. String args []
B. String [] args
C. Strings args []
D. String args
Q. 5 ]What does the zeroth element of the string array passed to the public static void main method contain?
A. The name of the program
B. The number of arguments
C. The first argument if one is present
Q. 6 ]Which of the following are Java keywords?
A. goto
B. malloc
C. extends
D. FALSE
Q. 7 ]What will be the result of compiling the following code:
public class Test {
public static void main (String args []) {
int age;
age = age + 1;
System.out.println("The age is " + age);
}}
A. Compiles and runs with no output
B. Compiles and runs printing out The age is 1
C. Compiles but generates a runtime error
D. Does not compile
E. Compiles but generates a compile time error
Q. 8 ]Which of these is the correct format to use to create the literal char value a?
A. 'a'
B. "a"
C. new Character(a)
D. \000a
Q. 9 ]What is the legal range of a byte integral type?
A. 0 - 65, 535
B. (-128) - 127
C. (-32,768) - 32,767
D. (-256) - 255
Q. 10]Which of the following is illegal:
A. int i = 32;
B. float f = 45.0;
C. double d = 45.0;
Q. 11]What will be the result of compiling the following code:
public class Test {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}}
A. Compiles and runs with no output
B. Compiles and runs printing out The age is 1
C. Compiles but generates a runtime error
D. Does not compile
E. Compiles but generates a compile time error
Q. 12]Which of the following are correct?
A. 128 >> 1 gives 64
B. 128 >>> 1 gives 64
C. 128 >> 1 gives -64
D. 128 >>> 1 gives -64
Q. 13]Which of the following return true?
A. "john" == "john"
B. "john".equals("john")
C. "john" = "john"
D. "john".equals(new Button("john"))
Q. 14]Which of the following do not lead to a runtime error?
A. "john" + " was " + " here"
B. "john" + 3
C. 3 + 5
D. 5 + 5.5
Q. 15]Which of the following are so called "short circuit" logical operators?
A. &
B. ||
C. &&
D. |
Q. 16]Which of the following are acceptable?
A. Object o = new Button("A");
B. Boolean flag = true;
C. Panel p = new Frame();
D. Frame f = new Panel();
E. Panel p = new Applet();
Q. 17]What is the result of compiling and running the following code:
public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}}
A. The class will not compile
B. The compiler reports and error at line 2
C. The compiler reports an error at line 9
D. The value 10 is one of the elements printed to the standard output
E. The class compiles but generates a runtime error
Q. 18]Which of the following is correct:
A. String temp [] = new String {"j" "a" "z"};
B. String temp [] = { "j " " b" "c"};
C. String temp = {"a", "b", "c"};
D. String temp [] = {"a", "b", "c"};
Q. 19]What is the correct declaration of an abstract method that is intended to be public:
A. public abstract void add();
B. public abstract void add() {}
C. public abstract add();
D. public virtual add();
Q. 20]Under what situations do you obtain a default constructor?
A. When you define any class
B. When the class has no other constructors
C. When you define at least one constructor
Q. 21]Given the following code:
public class Test { … }
Which of the following can be used to define a constructor for this class:
A. public void Test() {…}
B. public Test() {…}
C. public static Test() {…}
D. public static void Test() {…}
Q. 22]Which of the following are acceptable to the Java compiler:
A. if (2 == 3) System.out.println("Hi");
B. if (2 = 3) System.out.println("Hi");
C. if (true) System.out.println("Hi");
D. if (2 != 3) System.out.println("Hi");
E. if (aString.equals("hello")) System.out.println("Hi");
Q. 23]Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is the correct way for a method to indicate that it expects the caller to handle that exception:
A. throw Exception
B. throws Exception
C. new Exception
D. Don't need to specify anything
Q. 24]What is the result of executing the following code, using the parameters 4 and 0:
public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}
A. Prints out: Exception Finally
B. Prints out: Finally
C. Prints out: Exception
D. No output
Q. 25]Which of the following is a legal return type of a method overloading the following method:
public void add(int a) {…}
A. void
B. int
C. Can be anything
Q. 26]Which of the following statements is correct for a method which is overriding the following method:
public void add(int a) {…}
A. the overriding method must return void
B. the overriding method must return int
C. the overriding method can return whatever it likes
Q. 27]Given the following classes defined in separate files:
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}}
class Car extends Vehicle {
public void drive() {
System.out.println("Car: drive");
}}
public class Test {
public static void main (String args []) {
Vehicle v;
Car c;
v = new Vehicle();
c = new Car();
v.drive();
c.drive();
v = c;
v.drive();
}}
What will be the effect of compiling and running this class Test?
A. Generates a Compiler error on the statement v= c;
B. Generates runtime error on the statement v= c;
C. Prints out:
Vehicle: drive
Car: drive
Car: drive
D. Prints out:
Vehicle: drive
Car: drive
Vehicle: drive
Q. 28]Where in a constructor, can you place a call to a constructor defined in the super class?
A. Anywhere
B. The first statement in the constructor
C. The last statement in the constructor
D. You can't call super in a constructor
Q. 29]Which variables can an inner class access from the class which encapsulates it?
A. All static variables
B. All final variables
C. All instance variables
D. Only final instance variables
E. Only final static variables
Q. 30]What class must an inner class extend:
A. The top level class
B. The Object class
C. Any class or interface
D. It must extend an interface
Q. 31]In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:
1. public class Test {
2. public static void main (String args []) {
3. Employee e = new Employee("Bob", 48);
4. e.calculatePay();
5. System.out.println(e.printDetails());
6. e = null;
7. e = new Employee("Denise", 36);
8. e.calculatePay();
9. System.out.println(e.printDetails());
10. }
11. }
A. Line 10
B. Line 11
C. Line 7
D. Line 8
E. Never
Q. 32]What is the name of the interface that can be used to define a class that can execute within its own thread?
A. Runnable
B. Run
C. Threadable
D. Thread
E. Executable
Q. 33]What is the name of the method used to schedule a thread for execution?
A. init();
B. start();
C. run();
D. resume();
E. sleep();
Q. 34]Which methods may cause a thread to stop executing?
A. sleep();
B. stop();
C. yield();
D. wait();
E. notify();
F. notifyAll()
G. synchronized()
Q. 35]Examine the following code:
public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}}
Does this code:
A. Produce a runtime error
B. Produce a compile time error
C. Print out "Total 0"
D. Generate the following as output:
i = 0 : j = 10
i = 1 : j = 9
i = 2 : j = 8
Total 30
Q. 36]Which of the following methods are defined on the Graphics class:
A. drawLine(int, int, int, int)
B. drawImage(Image, int, int, ImageObserver)
C. drawString(String, int, int)
D. add(Component);
E. setVisible(boolean);
F. setLayout(Object);
Q. 37]Which of the following layout managers honours the preferred size of a component:
A. CardLayout
B. FlowLayout
C. BorderLayout
D. GridLayout
Q. 38]Given the following code what is the effect of a being 5:
public class Test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
System.out.println(i * j);
}}}}
A. Generate a runtime error
B. Throw an ArrayIndexOutOfBoundsException
C. Print the values: 1, 2, 2, 4
D. Produces no output
Q. 39]What is the effect of issuing a wait() method on an object
A. If a notify() method has already been sent to that object then it has no effect
B. The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
C. An exception will be raised
D. The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
Q. 40]The layout of a container can be altered using which of the following methods:
A. setLayout(aLayoutManager);
B. addLayout(aLayoutManager);
C. layout(aLayoutManager);
D. setLayoutManager(aLayoutManager);
Q. 41]Using a FlowLayout manager, which is the correct way to add elements to a container:
A. add(component);
B. add("Center", component);
C. add(x, y, component);
D. set(component);
Q. 42]Given that a Button can generate an ActionEvent which listener would you expect to have to implement, in a class which would handle this event?
A. FocusListener
B. ComponentListener
C. WindowListener
D. ActionListener
E. ItemListener
Q. 43]Which of the following, are valid return types, for listener methods:
A. boolean
B. the type of event handled
C. void
D. Component
Q. 44]Assuming we have a class which implements the ActionListener interface, which method should be used to register this with a Button?
A. addListener(*);
B. addActionListener(*);
C. addButtonListener(*);
D. setListener(*);
Q. 45]In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:
A. paint()
B. repaint()
C. paint(Graphics)
D. update(Graphics)
E. None - you should never cause paint(Graphics) to execute
Q. 46]Which of the following illustrates the correct way to pass a parameter into an applet:
A. <applet code=Test.class age=33 width=100 height=100>
B. <param name=age value=33>
C. <applet code=Test.class name=age value=33 width=100 height=100>
D. <applet Test 33>
Q. 47]Which of the following correctly illustrate how an InputStreamReader can be created:
A. new InputStreamReader(new FileInputStream("data"));
B. new InputStreamReader(new FileReader("data"));
C. new InputStreamReader(new BufferedReader("data"));
D. new InputStreamReader("data");
E. new InputStreamReader(System.in);
Q. 48]What is the permanent effect on the file system of writing data to a new FileWriter("report"), given the file report already exists?
A. The data is appended to the file
B. The file is replaced with a new file
C. An exception is raised as the file already exists
D. The data is written to random locations within the file
Q. 49]What is the effect of adding the sixth element to a vector created in the following manner:
new Vector(5, 10);
A. An IndexOutOfBounds exception is raised.
B. The vector grows in size to a capacity of 10 elements
C. The vector grows in size to a capacity of 15 elements
D. Nothing, the vector will have grown when the fifth element was added
Q. 50]What is the result of executing the following code when the value of x is 2:
switch (x) {
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
A. Nothing is printed out
B. The value 3 is printed out
C. The values 3 and 4 are printed out
D. The values 1, 3 and 4 are printed out
Q. 51]Consider the following example:
class First {
public First (String s) {
System.out.println(s);
}}
public class Second extends First {
public static void main(String args []) {
new Second();
}}
What is the result of compiling and running the Second class?
A. Nothing happens
B. A string is printed to the standard out
C. An instance of the class First is generated
D. An instance of the class Second is created
E. An exception is raised at runtime stating that there is no null parameter constructor in class First.
F. The class second will not compile as there is no null parameter constructor in the class First
Q. 52]What is the result of executing the following fragment of code:
boolean flag = false;
if (flag = true) {
System.out.println("true");
} else {
System.out.println("false");
}
A. true is printed to standard out
B. false is printed to standard out
C. An exception is raised
D. Nothing happens
Q. 53]Consider the following classes:
public class Test {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String args []) {
test();
}}
What is the result of compiling and running this class?
A. The string Test is printed to the standard out.
B. A runtime exception is raised stating that an object has not been created.
C. Nothing is printed to the standard output.
D. An exception is raised stating that the method test cannot be found.
E. An exception is raised stating that the variable this can only be used within an instance.
F. The class fails to compile stating that the variable this is undefined.
Q. 54]Examine the following class definition:
public class Test {
public static void test() {
print();
}
public static void print() {
System.out.println("Test");
}
public void print() {
System.out.println("Another Test");
}}
What is the result of compiling this class:
A. A successful compilation.
B. A warning stating that the class has no main method.
C. An error stating that there is a duplicated method.
D. An error stating that the method test() will call one or other of the print() methods.
Q. 55]What is the result of compiling and executing the following Java class:
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
suspend();
resume();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. Compilation will fail in the method main.
B. Compilation will fail in the method run.
C. A warning will be generated for method run.
D. The string "In run" will be printed to standard out.
E. Both strings will be printed to standard out.
F. Nothing will happen.
Q. 56]Given the following sequence of Java statements
1. StringBuffer sb = new StringBuffer("abc");
2. String s = new String("abc");
3. sb.append("def");
4. s.append("def");
5. sb.insert(1, "zzz");
6. s.concat(sb);
7. s.trim();
Which of the following statements are true:
A. The compiler would generate an error for line 1.
B. The compiler would generate an error for line 2.
C. The compiler would generate an error for line 3.
D. The compiler would generate an error for line 4.
E. The compiler would generate an error for line 5.
F. The compiler would generate an error for line 6.
G. The compiler would generate an error for line 7.
Q. 57]What is the result of executing the following Java class:
import java.awt.*;
public class FrameTest extends Frame {
public FrameTest() {
add (new Button("First"));
add (new Button("Second"));
add (new Button("Third"));
pack();
setVisible(true);
}
public static void main(String args []) {
new FrameTest();
}}
Select from the following options:
A. Nothing happens.
B. Three buttons are displayed across a window.
C. A runtime exception is generated (no layout manager specified).
D. Only the "first" button is displayed.
E. Only the "second" button is displayed.
F. Only the "third" button is displayed.
Q. 58]Consider the following tags and attributes of tags:
1. CODEBASE
2. ALT
3. NAME
4. CLASS
5. JAVAC
6. HORIZONTALSPACE
7. VERTICALSPACE
8. WIDTH
9. PARAM
10. JAR
Which of the above can be used within the <APPLET> and </APPLET> tags?
A. line 1, 2, 3
B. line 2, 5, 6, 7
C. line 3, 4, 5
D. line 8, 9, 10
E. line 8, 9
Q. 59]Which of the following is a legal way to construct a RandomAccessFile:
A. RandomAccessFile("data", "r");
B. RandomAccessFile("r", "data");
C. RandomAccessFile("data", "read");
D. RandomAccessFile("read", "data");
Q. 60]Carefully examine the following code:
public class StaticTest {
static {
System.out.println("Hi there");
}
public void print() {
System.out.println("Hello");
}
public static void main(String args []) {
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}}
When will the string "Hi there" be printed?
A. Never.
B. Each time a new instance is created.
C. Once when the class is first loaded into the Java virtual machine.
D. Only when the static method is called explicitly.
Q. 61]Consider the following program:
public class Test { public static void main (String args []) { boolean a = false; if (a = true)
System.out.println("Hello");
Else
System.out.println("Goodbye");
}
}
What is the result:
A. Program produces no output but terminates correctly.
B. Program does not terminate.
C. Prints out "Hello"
D. Prints out "Goodbye"
Q. 62]Examine the following code which includes an inner class:
public final class Test4 implements A {
class Inner {
void test() {
if (Test4.this.flag); {sample();}
}
}
private boolean flag = false;
public void sample() {
System.out.println("Sample");
}
public Test4() {(new Inner()).test();}
public static void main(String args []) {
new Test4();
}}
What is the result:
A. Prints out "Sample"
B. Program produces no output but terminates correctly.
C. Program does not terminate.
D. The program will not compile
Q. 63]Carefully examine the following class:
public class Test5 { public static void main (String args []) { /* This is the start of a comment
if (true) {
Test5 = new test5();
System.out.println("Done the test");
}
/* This is another comment */
System.out.println ("The end");
}}
What is the result:
A. Prints out "Done the test" and nothing else.
B. Program produces no output but terminates correctly.
C. Program does not terminate.
D. The program will not compile.
E. The program generates a runtime exception.
F. The program prints out "The end" and nothing else.
G. The program prints out "Done the test" and "The end"
Q. 64]The following code defines a simple applet:
import java.applet.Applet;
import java.awt.*;
public class Sample extends Applet {
private String text = "Hello World";
public void init() {
add(new Label(text));
}
public Sample (String string) {
text = string;
}}
It is accessed form the following HTML page:
<html>
<title>Sample Applet</title>
<body>
<applet code="Sample.class" width=200 height=200></applet>
</body>
</html>
What is the result of compiling and running this applet:
A. Prints "Hello World".
B. Generates a runtime error.
C. Does nothing.
D. Generates a compile time error.