Java Certification Mock Exam

The certification for the Java programmer and developer courses are done by Sun approved independed institutes (more info see Sun educational Services). About this Mock exams the following credits:

Java Certification Mock Exam

by John Hunt

Email: jjh@aber.ac.uk

For as long as it will be there you' ll find the original document here.
I made a copy and you'll find it in the following section.

This mock examination consists of 60 questions (as does the actual exam).

Just as in the actual examination a mixture of style of questions are included. These range between questions that require a single answer and those which require 1 or more answers. The former are indicated by:

    Note a question that has the above request may require only one option, it is for you to decide. However, if you do not identify all the options for that question, then you will score zero for that question. One free format question is included as an example of that style of questioning.

    As in the actual exam you should not attempt to identify any trends relating to As, Bs or Cs. That is, do not assume that because the answer A has not appeared for a while there is a good chance that it will soon.

    For the actual examination the pass mark is 70% you should therefore aim to achieve at least 35 correct answers in this mock examination.

    The correct answers are give at the end of the examination.

    Disclaimer: This mock examination is in no way sanctioned by Sun Microsystems and no guarantees are provided about the similarity of these questions to those in the actual exam.

 

Question 1

Which colour is used to indicate instance methods in the standard "javadoc" format documentation:

  1. blue
  2. red
  3. purple
  4. orange

Select the most appropriate answer.

 

Question 2

What is the correct ordering for the import, class and package declarations when found in a single file?

  1. package, import, class
  2. class, import, package
  3. import, package, class
  4. package, class, import

Select the most appropriate answer.

 

Question 3

Which methods can be legally applied to a string object?

  1. equals(String)
  2. equals(Object)
  3. trim()
  4. round()
  5. toString()

Select all correct answers.

 

Question 4

What is the parameter specification for the public static void main method?

  1. String args []
  2. String [] args
  3. Strings args []
  4. String args

Select all correct answers.

 

Question 5

What does the zeroth element of the string array passed to the public static void main method contain?

  1. The name of the program
  2. The number of arguments
  3. The first argument if one is present

Select the most appropriate answer.

 

Question 6

Which of the following are Java keywords (as opposed to reserved words)?

  1. goto
  2. malloc
  3. extends
  4. FALSE

Select all correct answers

 

Question 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);
	}
}
  1. Compiles and runs with no output
  2. Compiles and runs printing out The age is 1
  3. Compiles but generates a runtime error
  4. Does not compile

Select the most appropriate answer.

 

Question 8

Which of these is the correct format to use to create the literal char value a?

  1. ‘a’
  2. "a"
  3. new Character(a)
  4. \000a

Select the most appropriate answer.

 

Question 9

What is the legal range of a byte integral type?

  1. 0 - 65, 535
  2. (–128) – 127
  3. (–32,768) – 32,767
  4. (–256) – 255

Select the most appropriate answer.

 

Question 10

Which of the following is illegal:

  1. int i = 32;
  2. float f = 45.0;
  3. double d = 45.0;

Select the most appropriate answer.

 

Question 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); 
 	} 
 } 
  1. Compiles and runs with no output
  2. Compiles and runs printing out The age is 1
  3. Compiles but generates a runtime error
  4. Does not compile
  5. Compiles but generates a compile time error

Select the most appropriate answer.

 

Question 12

Which of the following are correct?

  1. 128 >> 1 gives 64
  2. 128 >>> 1 gives 64
  3. 128 >> 1 gives –64
  4. 128 >>> 1 gives –64

Select all correct answers

 

Question 13

Which of the following return true?

  1. "john" == "john"
  2. "john".equals("john")
  3. "john" = "john"
  4. "john".equals(new Button("john"))

Select all correct answers.

 

Question 14

Which of the following do not lead to a runtime error?

  1. "john" + " was " + " here"
  2. "john" + 3
  3. 3 + 5
  4. 5 + 5.5

Select all correct answers.

 

Question 15

Which of the following are so called "short circuit" logical operators?

  1. &
  2. ||
  3. &&
  4. |

Select all correct answers.

 

Question 16

Which of the following are acceptable?

  1. Object o = new Button("A");
  2. Boolean flag = true;
  3. Panel p = new Frame();
  4. Frame f = new Panel();
  5. Panel p = new Applet();

Select all correct answers.

 

Question 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); 
 		} 
 	} 
 } 
  1. The class will not compile
  2. The compiler reports and error at line 2
  3. The compiler reports an error at line 9
  4. The value 10 is one of the elements printed to the standard output
  5. The class compiles but generates a runtime error

 

Select all correct answers.

 

Question 18

Which of the following is correct:

  1. String temp [] = new String {"j" "a" "z"};
  2. String temp [] = { "j " " b" "c"}
  3. String temp = {"a", "b", "c"}
  4. String temp [] = {"a", "b", "c"}

Select the most appropriate answer.

 

Question 19

What is the correct declaration of an abstract method that is intended to be public:

  1. public abstract void add();
  2. public abstract void add() {}
  3. public abstract add();
  4. public virtual add();

Select the most appropriate answer.

 

Question 20

Under what situations do you obtain a default constructor?

  1. When you define any class
  2. When the class has no other constructors
  3. When you define at least one constructor

Select the most appropriate answer.

 

Question 21

Given the following code:

 

 public class Test { 
     … 
 } 

Which of the following can be used to define a constructor for this class:

  1. public void Test() {…}
  2. public Test() {…}
  3. public static Test() {…}
  4. public static void Test() {…}

Select the most appropriate answer.

 

Question 22

Which of the following are acceptable to the Java compiler:

  1. if (2 == 3) System.out.println("Hi");
  2. if (2 = 3) System.out.println("Hi");
  3. if (true) System.out.println("Hi");
  4. if (2 != 3) System.out.println("Hi");
  5. if (aString.equals("hello")) System.out.println("Hi");

Select all correct answers.

 

Question 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 does not handle that exception:

  1. throw Exception
  2. throws Exception
  3. new Exception
  4. Don't need to specify anything

Select the most appropriate answer.

 

Question 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"); 
 } 
  1. Prints out: Exception Finally
  2. Prints out: Finally
  3. Prints out: Exception
  4. No output

Select the most appropriate answer.

 

Question 25

Which of the following is a legal return type of a method overloading the following method:

public void add(int a) {…}
  1. void
  2. int
  3. Can be anything

Select the most appropriate answer.

 

Question 26

Which of the following statements is correct for a method which is overriding the following method:

public void add(int a) {…}
  1. the overriding method must return void
  2. the overriding method must return int
  3. the overriding method can return whatever it likes

Select the most appropriate answer.

 

Question 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?

  1. Generates a Compiler error on the statement v= c;
  2. Generates runtime error on the statement v= c;
  3. Prints out:
  4. Vehicle: drive

    Car: drive

    Car: drive

  5. Prints out:

Vehicle: drive

Car: drive

Vehicle: drive

Select the most appropriate answer.

 

Question 28

Where in a constructor, can you place a call to a constructor defined in the super class?

  1. Anywhere
  2. The first statement in the constructor
  3. The last statement in the constructor
  4. You can't call super in a constructor

Select the most appropriate answer.

 

Question 29

Which variables can an inner class access from the class which encapsulates it?

  1. All static variables
  2. All final variables
  3. All instance variables
  4. Only final instance variables
  5. Only final static variables

Select all correct answers.

 

Question 30

What class must an inner class extend:

  1. The top level class
  2. The Object class
  3. Any class or interface
  4. It must extend an interface

Select the most appropriate answer.

 

Question 31

In the following code which is the earliest statement, where the object originally held in employee, may be garbage collected:

 public class Test { 
  public static void main (String args []) { 
    Employee e = new Employee("Bob", 48); 
    e.calculatePay(); 
    System.out.println(e.printDetails()); 
    e = null; 
    e = new Employee("Denise", 36); 
    e.calculatePay(); 
    System.out.println(e.printDetails()); 
  } 
 } 
  1. Line 10
  2. Line 11
  3. Line 7
  4. Line 8
  5. Never

Select the most appropriate answer.

 

Question 32

What is the name of the interface that can be used to define a class that can execute within its own thread?

  1. Runnable
  2. Run
  3. Threadable
  4. Thread
  5. Executable

Select the most appropriate answer.

 

Question 33

What is the name of the method used to schedule a thread for execution?

  1. init();
  2. start();
  3. run();
  4. resume();
  5. sleep();

Select the most appropriate answer.

 

Question 34

Which methods may cause a thread to stop executing?

  1. sleep();
  2. stop();
  3. yield();
  4. wait();
  5. notify();
  6. notifyAll()
  7. synchronized()

Select all correct answers.

 

Question 35

Write code to create a text area able to display 10 characters (assuming a fixed size font) displaying the initial string "hello":

 

Question 36

Which of the following methods are defined on the Graphics class:

  1. drawLine(int, int, int, int)
  2. drawImage(Image, int, int, ImageObserver)
  3. drawString(String, int, int)
  4. add(Component);
  5. setVisible(boolean);
  6. setLayout(Object);

Select all correct answers.

 

Question 37

Which of the following layout managers honours the preferred size of a component:

  1. CardLayout
  2. FlowLayout
  3. BorderLayout
  4. GridLayout

Select all correct answers.

 

Question 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); 
 	            } 
 	    } 
 	} 
 } 
  1. Generate a runtime error
  2. Throw an out of bounds exception
  3. Print the values: 1, 2, 2, 4
  4. Produces no output

Select the most appropriate answer.

 

Question 39

What is the effect of issuing a wait() method on an object

  1. If a notify() method has already been sent to that object then it has no effect
  2. The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
  3. An exception will be raised
  4. The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.

Select the most appropriate answer.

 

Question 40

The layout of a container can be altered using which of the following methods:

  1. setLayout(aLayoutManager);
  2. addLayout(aLayoutManager);
  3. layout(aLayoutManager);
  4. setLayoutManager(aLayoutManager);

Select all correct answers.

 

Question 41

Using a FlowLayout manager, which is the correct way to add elements to a container:

  1. add(component);
  2. add("Center", component);
  3. add(x, y, component);
  4. set(component);

Select the most appropriate answer.

 

Question 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?

  1. FocusListener
  2. ComponentListener
  3. WindowListener
  4. ActionListener
  5. ItemListener

Select the most appropriate answer.

 

Question 43

Which of the following, are valid return types, for listener methods:

  1. boolean
  2. the type of event handled
  3. void
  4. Component

Select the most appropriate answer.

 

Question 44

Assuming we have a class which implements the ActionListener interface, which method should be used to register this with a Button?

  1. addListener(*);
  2. addActionListener(*);
  3. addButtonListener(*);
  4. setListener(*);

Select the most appropriate answer.

 

Question 45

In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:

  1. paint()
  2. repaint()
  3. paint(Graphics)
  4. update(Graphics)
  5. None – you should never cause paint(Graphics) to execute

Select the most appropriate answer.

 

Question 46

Which of the following illustrates the correct way to pass a parameter into an applet:

  1. <applet code=Test.class age=33 width=100 height=100>
  2. <param name=age value=33>
  3. <applet code=Test.class name=age value=33 width=100 height=100>
  4. <applet Test 33>

Select the most appropriate answer.

 

Question 47

Which of the following correctly illustrates how an InputStreamReader can be created:

  1. new InputStreamReader(new FileInputStream("data"));
  2. new InputStreamReader(new FileReader("data"));
  3. new InputStreamReader(new BufferedReader("data"));
  4. new InputStreamReader("data");
  5. new InputStreamReader(System.in);

Select all correct answers.

 

Question 48

What is the permanent effect on the file system of writing data to a new FileWriter("report"), given the file report already exists;?

  1. The data is appended to the file
  2. The file is replaced with a new file
  3. An exception is raised as the file already exists
  4. The data is written to random locations within the file

Select the most appropriate answer.

 

Question 49

What is the effect of adding the sixth element to a vector created in the following manner:

new Vector(5, 10);
  1. An IndexOutOfBounds exception is raised.
  2. The vector grows in size to a capacity of 10 elements
  3. The vector grows in size to a capacity of 15 elements
  4. Nothing, the vector will have grown when the fifth element was added

Select the most appropriate answer.

 

Question 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); 
 } 
  1. Nothing is printed out
  2. The value 3 is printed out
  3. The values 3 and 4 are printed out
  4. The values 1, 3 and 4 are printed out

Select the most appropriate answer.

 

Question 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?

  1. Nothing happens
  2. A string is printed to the standard out
  3. An instance of the class First is generated
  4. An instance of the class Second is created
  5. An exception is raised at runtime stating that there is no null parameter constructor in class First.
  6. The class second will not compile as there is no null parameter constructor in the class First

Select the most appropriate answer.

 

Question 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"); 
 } 
  1. true is printed to standard out
  2. false is printed to standard out
  3. An exception is raised
  4. Nothing happens

Select the most appropriate answer.

 

Question 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?

  1. The string Test is printed to the standard out.
  2. A runtime exception is raised stating that an object has not been created.
  3. Nothing is printed to the standard output.
  4. An exception is raised stating that the method test cannot be found.
  5. An exception is raised stating that the variable this can only be used within an instance.
  6. The class fails to compile stating that the variable this is undefined.

Select all correct answers.

 

Question 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:

  1. A successful compilation.
  2. A warning stating that the class has no main method.
  3. An error stating that there is a duplicated method.
  4. An error stating that the method test() will call one or other of the print() methods.

Select the most appropriate answer.

 

Question 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(); 
     } 
 } 
  1. Compilation will fail in the method main.
  2. Compilation will fail in the method run.
  3. A warning will be generated for method run.
  4. The string "In run" will be printed to standard out.
  5. Both strings will be printed to standard out.
  6. Nothing will happen.

Select the most appropriate answer.

 

Question 56

Given the following sequence of Java statements

Which of the following statements are true:

  1. The compiler would generate an error for line 1.
  2. The compiler would generate an error for line 2.
  3. The compiler would generate an error for line 3.
  4. The compiler would generate an error for line 4.
  5. The compiler would generate an error for line 5.
  6. The compiler would generate an error for line 6.
  7. The compiler would generate an error for line 7.

 

Select all correct answers.

 

Question 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(); 
    } 
 } 

What is the result:

  1. Nothing happens.
  2. Three buttons are displayed across a window.
  3. A runtime exception is generated (no layout manager specified).
  4. Only the "first" button is displayed.
  5. Only the "second" button is displayed.
  6. Only the "third" button is displayed.

 

Select the most appropriate answer.

 

Question 58

Consider the following 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 are tags that can be used within the APPLET tag?

  1. line 1, 2, 3
  2. line 2, 5, 6, 7
  3. line 3, 4, 5
  4. line 8, 9, 10
  5. line 8, 9

 

Select all correct answers.

 

Question 59

Which of the following are legal ways to construct a RandomAccessFile:

  1. RandomAccessFile("data", "r");
  2. RandomAccessFile("r", "data");
  3. RandomAccessFile("data", "read");
  4. RandomAccessFile("read", "data");

 

Select the most appropriate answer.

 

Question 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?

  1. Never.
  2. Each time a new instance is created.
  3. Once when the class is first loaded into the Java virtual machine.
  4. Only when the static method is called explicitly.

Select the most appropriate answer.

To top of document

 

{submenus}
DISCLAIMER
   
 

 

1