Java Examination (Include Answer)
Java Examination (Include Answer)
Question 1: What is the correct ordering for the import, class and package declarations when found
in a single file?
A. equals(String)
B. equals(Object)
C. trim()
D. round()
A. The loop initializer uses the correct form to set value of i and j
B. The loop initializer is not legal and code will not compile for that reason
C. If we put the following statement after the loop, it would report “i = 5 j = 5”
System.out.println(“i = ” + i + “ j = ” + j);
System.out.println(“ j = ” + j);
Question 5: 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
Question 6: What happen when we try to compile and run code containing the following lines?
String s = “12345”;
String t = new String(s);
if(s == t then) System.out.println(t + “==” + s);
else System.out.println(t + “!=” + s);
A. 0 - 65, 535
B. (–128) – 127
C. (–32,768) – 32,767
D. (–256) – 255
A. int i = 32;
B. float f = 45.0;
C. long l = 40;
D. double d = 45.0;
A. float f = -412;
B. double d = 0x12345678;
C. short s = 10;
D. int other = (int)true;
A. 0x12
B. 32O
C. 032
D. (octal)2
Question 13: What is the result of compiling and running the following code:
Question 17: Which of the following are not acceptable to the Java compiler:
A. if (2 == 3) System.out.println("Hi");
B. if (2 = 3) System.out.println("Hi");
C. if (2 != 3) System.out.println("Hi");
D. if (aString.equals("hello")) System.out.println("Hi");
Question 18: 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 does not handle that
exception
A. throw Exception
B. new Exception
C. throws Exception
D. Don't need to specify anything
Question 19: What is the result of executing the following code, using the parameters 4 and 0
What will be the effect of compiling and running this class Test?
Question 21: 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
A. Line 10
B. Line 11
C. Line 7
D. Line 8
Question 24: Which of the following methods are not defined on the Graphics class:
Question 25: Which of the following layout managers honours the preferred size of a component:
A. CardLayout
B. FlowLayout
C. BorderLayout
D. GridLayout
Question 26: Given the following code what is the effect of a being 5:
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.
Question 28: 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);
Question 29: 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);
Question 30: 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. ItemListener
D. ActionListener
Question 31: 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(*);
Question 32: 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)
Question 33: What Graphics methods will draw the outline of a square?
A. drawRect()
B. fillRect()
C. drawRectangle()
D. drawSquare()
Question 34: The setForeground() and setBackground() methods are defined in class
A. Graphics
B. Container
C. Component
D. Object
Question 35: Which of the following illustrates the correct way to pass a parameter into an applet:
Question 37: What is the permanent effect on the file system of writing data to a new
FileWriter("report"), given the file report already exists;?
Question 38: What is the effect of adding the sixth element to a vector created in the following
manner:
Question 39: The Vector class provides storage for object references in the order of addition and
automatically epands as needed. Which of the following classes is closed in function to the Vector
class?
A. java.util.ArrayList
B. java.util.Hashtable
C. java.util.LinkedList
D. java.util.List
Question 40: 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);
}
class First {
public First (String s) {
System.out.println(s);
}
}
public class Second extends First {
public static void main(String args []) {
new Second();
}
}
A. Nothing happens
B. An instance of the class Second is created
C. An exception is raised at runtime stating that there is no null parameter constructor in class
First.
D. The class second will not compile as there is no null parameter constructor in the
class First
Question 42: What is the result of executing the following fragment of code:
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.
Question 46: 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();
}
}
Question 47: Which of the following are legal ways to construct a RandomAccessFile:
A. RandomAccessFile("data", "r");
B. RandomAccessFile("r", "data");
C. RandomAccessFile("data", "read");
D. RandomAccessFile("read", "data");
Question 48: If you run the following code on a on a PC from the directory c:\source:
import java.io.*;
class Path {
public static void main(String[] args) throws Exception
{
File file = new File("Ran.test");
System.out.println(file.getAbsolutePath());
}
}
A. Ran.test
B. source\Ran.test
C. c:\source\Ran.test
D. c:\source
Question 49: Carefully examine the following code:
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.
Question 50: Question 3: What is the proper way of defining a class named Key so that it cannot be
subclassed?
Question 51: 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. Thread
D. Executable
Question 52: What is the name of the method used to schedule a thread for execution?
A. init();
B. start();
C. run();
D. resume();
Question 53: All methods may cause a thread to stop executing. Except?
A. sleep();
B. yield();
C. wait();
D. notify();
Question 54: Question 46: If you supply a target object when you create a new Thread, as in:
Question 55: What is the result of compiling and executing the following Java class:
Question 56: What is the result of compiling and executing the following Java class:
import java.io.*;
class MyClass{
}
public class SaveState {
public static void main(String argv[]) throws
IOException,ClassNotFoundException{
new SaveState();
}
SaveState()throws IOException{
MyClass mc = new MyClass();
FileOutputStream out = new FileOutputStream("state.ob");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(mc);
}
}