JAVA Programming Questions & Answers (www.4tests.com)
| 1) | Which of the following is illegal? |
|
|
|
| int i = 32; | |
| float f = 45.0; | |
| double d = 45.0; | |
float f = 45.0;
| 2) | public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); } } |
|
|
|
| Compiles and runs with no output | |
| Compiles and runs printing out The age is 1 | |
| Compiles but generates a runtime error | |
| Does not compile | |
| Compiles but generates a compile time error | |
| Compiles and runs printing out The age is 1 |
| 3) | Which of the following are correct? |
|
|
|
| 128 >> 1 gives 64 | |
| 128 >>> 1 gives 64 | |
| 128 >> 1 gives -64 | |
| 128 >>> 1 gives -64 | |
| 128 >> 1 gives 64 | |
| 128 >>> 1 gives 64 |
| 4) | Which of the following return true? |
|
|
|
| "john" == "john" | |
| "john".equals("john") | |
| "john" = "john" | |
| "john".equals(new Button("john")) | |
|
|
|
| "john" == "john" | |
| "john".equals("john") |
| 5) | Which of the following are so called "short circuit" logical operators? |
|
|
|
| & | |
| || | |
| && | |
| | | |
| || | |
| && |
| 6) | Which of the following are acceptable? |
|
|
|
| Object o = new Button("A"); | |
| Frame f = new Panel(); | |
| Boolean flag = true; | |
| Boolean flag = true; | |
| Panel p = new Applet(); | |
Object o = new Button("A");
Panel p = new Applet();
| 7) | 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); }}} |
|
|
|
| The compiler reports an error at line 2 | |
| The class will not compile | |
| The value 10 is one of the elements printed to the standard output | |
| The compiler reports an error at line 9 | |
| The class compiles but generates a runtime error | |
The value 10 is one of the elements printed to the standard output
| 8) | Which of the following is correct: |
|
|
|
| String temp [] = new String {"j" "a" "z"}; | |
| String temp [] = { "j " " b" "c"}; | |
| String temp = {"a", "b", "c"}; | |
| String temp [] = {"a", "b", "c"}; | |
String temp [] = {"a", "b", "c"};
| 9) | What is the correct declaration of an abstract method that is intended to be public: |
|
|
|
| public abstract void add(); | |
| public abstract void add() {} | |
| public virtual add(); | |
| public abstract add(); | |
public abstract void add();
| 10) | Under what situations do you obtain a default constructor? |
|
|
|
| When you define any class | |
| When the class has no other constructors | |
| When you define at least one constructor | |
When the class has no other constructors
| 11) | Which of the following are acceptable to the Java compiler? |
|
|
|
| if (2 == 3) System.out.println("Hi"); | |
| if (2 = 3) System.out.println("Hi"); | |
| if (true) System.out.println("Hi"); | |
| if (2 != 3) System.out.println("Hi"); | |
| if (aString.equals("hello")) System.out.println("Hi"); | |
| 12) | Assuming a method contains code which may raise an Exception (but not a Runtime Exception), what is the correct way for a method to indicate that it expects the caller to handle that exception: |
|
|
|
| throw Exception | |
| throws Exception | |
| new Exception | |
| Don't need to specify anything | |
|
|
|
throws Exception
| 13) | public void divide(int a, int b) { try { int c = a / b; } catch (Exception e) { System.out.print("Exception "); } finally { System.out.println("Finally"); } |
|
|
|
| Prints out: Finally | |
| Prints out: Exception | |
| Prints out: Exception Finally | |
| No output | |
Prints out: Exception Finally
| 14) | Which of the following statements is
correct for a method which is overriding the following method: public void add(int a) {...} |
|
|
|
| the overriding method must return void | |
| the overriding method must return int | |
| the overriding method can return whatever it likes | |
the overriding method must return void
| 15) | 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? |
|
|
|
| Generates a Compiler error on the statement v= c | |
| Generates runtime error on the statement v= c | |
| Prints out: Vehicle : drive Car : drive Car : drive |
|
| Prints out: Vehicle : drive Car : drive Vehicle : drive |
|
Prints out:
Vehicle : drive
Car : drive
Car : drive
| 16) | Where in a constructor, can you place a call to a constructor defined in the super class? |
|
|
|
| The first statement in the constructor | |
| The last statement in the constructor | |
| You can't call super in a constructor | |
| Anywhere | |
The first statement in the constructor
| 17) | Which variables can an inner class access from the class which encapsulates it? |
|
|
|
| All static variables | |
| All final variables | |
| All instance variables | |
| Only final instance variables | |
| Only final static variables | |
|
|
|
| All static variables | |
| All final variables | |
| All instance variables | |
| 18) | What class must an inner class extend: |
|
|
|
| The top level class | |
| The Object class | |
| Any class or interface | |
| It must extend an interface | |
Any class or interface
| 19) | In the following code, which is the
earliest statement, where the object originally held in e, may be garbage
collected:
|
|
|
|
| Line 7 | |
| Line 8 | |
| Line 10 | |
| Line 11 | |
| Never | |
Line 7
| 20) | What is the name of the interface that can be used to define a class that can execute within its own thread? |
|
|
|
| Run | |
| Runnable | |
| Thread | |
| Threadable | |
| Executable | |
Runnable
| 21) | What is the name of the method used to schedule a thread for execution? |
|
|
|
| init(); | |
| start(); | |
| run(); | |
| resume(); | |
| sleep(); | |
start();
| 22) | Which methods may cause a thread to stop executing? |
|
|
|
| sleep(); | |
| stop(); | |
| yield(); | |
| wait(); | |
| notify(); | |
| sleep(); | |
| stop(); | |
| yield(); | |
| wait(); |
| 23) | Which of the following methods are defined on the Graphics class: |
|
|
|
| drawLine(int, int, int, int) | |
| drawImage(Image, int, int, ImageObserver) | |
| add(Component); | |
| drawString(String, int, int) | |
| setVisible(boolean); | |
| drawLine(int, int, int, int) | |
| drawImage(Image, int, int, ImageObserver) | |
| drawString(String, int, int) |
| 24) | Which of the following layout managers honors the preferred size of a component: |
|
|
|
| CardLayout | |
| FlowLayout | |
| BorderLayout | |
| GridLayout | |
|
|
|
FlowLayout
| 25) | 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); } } } } |
|
|
|
| Generate a runtime error | |
| Throw an ArrayIndexOutOfBoundsException | |
| Print the values: 1, 2, 2, 4 | |
| Produces no output | |
Produces no output
| 26) | What is the effect of issuing a wait() method on an object |
|
|
|
| If a notify() method has already been sent to that object then it has no effect | |
| The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method | |
| An exception will be raised | |
| The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object. | |
The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
| 27) | The layout of a container can be altered using which of the following methods: |
|
|
|
| setLayout(aLayoutManager); | |
| addLayout(aLayoutManager); | |
| layout(aLayoutManager); | |
| setLayoutManager(aLayoutManager); | |
setLayout(aLayoutManager);
| 28) | Using a FlowLayout manager, which is the correct way to add elements to a container: |
|
|
|
| set(component); | |
| add("Center", component); | |
| add(x, y, component); | |
| add(component); | |
add(component);
| 29) | 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? |
|
|
|
| FocusListener | |
| WindowListener | |
| ComponentListener | |
| ItemListener | |
| ActionListener | |
| ActionListener | |
|
|
|
| 30) | Which of the following, are valid return types, for listener methods: |
|
|
|
| boolean | |
| the type of event handled | |
| Component | |
| void | |
void
| 31) | Assuming we have a class which implements the ActionListener interface, which method should be used to register this with a Button? |
|
|
|
| addListener(*); | |
| addActionListener(*); | |
| addButtonListener(*); | |
| setListener(*); | |
addActionListener(*);
| 32) | In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call: |
|
|
|
| paint() | |
| repaint() | |
| paint(Graphics) | |
| update(Graphics) | |
| None, you should never cause paint(Graphics) to execute | |
repaint()
| 33) | Which of the following illustrates the correct way to pass a parameter into an applet: |
|
|
|
| <applet code=Test.class age=33 width=100 height=100> | |
| <param name=age value=33> | |
| <applet code=Test.class name=age value=33 width=100 height=100> | |
| <applet Test 33> | |
<param name=age value=33>
| 34) | Which of the following correctly illustrate how an InputStreamReader can be created: |
|
|
|
| new InputStreamReader(new FileInputStream("data")); | |
| new InputStreamReader(new BufferedReader("data")); | |
| new InputStreamReader(System.in); | |
| new InputStreamReader("data"); | |
| new InputStreamReader(new FileReader("data")); | |
new InputStreamReader(new FileInputStream("data"));
new InputStreamReader(System.in);
| 35) | What is the permanent effect on the file system of writing data to a new FileWriter("report"), given the file report already exists? |
|
|
|
| The data is appended to the file | |
| The file is replaced with a new file | |
| An exception is raised as the file already exists | |
| The data is written to random locations within the file | |
The file is replaced with a new file
| 36) | What is the effect of adding the
sixth element to a vector created in the following manner: new Vector(5, 10); |
|
|
|
| An IndexOutOfBounds exception is raised. | |
| The vector grows in size to a capacity of 10 elements | |
| The vector grows in size to a capacity of 15 elements | |
| Nothing, the vector will have grown when the fifth element was added | |
The vector grows in size to a capacity of 15 elements
| 37) | 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); |
|
|
|
| The value 3 is printed out | |
| The values 3 and 4 are printed out | |
| The values 1, 3 and 4 are printed out | |
| Nothing is printed out | |
The values 3 and 4 are printed out