Text Box: Share on
Facebook
Google+
Linked in
Email
Copyright 3S Technologies

Email: info@3s-technologies.com

Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.

Java Objectives - Part 9

The reason for documenting this topic is to provide important technical questions and answers on Java language. These questions can appear in an aptitude test, organized by different recruiting IT organizations.

 

Some questions and answers on AWT: CONTROLS, LAYOUT MANAGERS AND MENUS:

 

1) 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. }

 

1) Line 10

2) Line 11

3) Line 7

4) Line 8

5) Never

Ans: 3

 

2) 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

Ans: 1

 

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

1) init();

2) start();

3) run();

4) resume();

5) sleep();

Ans: 2

 

4) Which methods may cause a thread to stop executing? (Choose four)

1) sleep();

2) stop();

3) yield();

4) wait();

5) notify();

6) notifyAll()

7) synchronized()

Ans: 1,2,3,4

 

5) Which of the following would create a text field able to display 10 characters (assuming a fixed size font) displaying the initial string "hello":

1) new TextField("hello", 10);

2) new TextField("hello");

3) new textField(10);

4) new TextField();

Ans: 1

 

6) Which of the following methods are defined on the Graphics class ? (Choose three)

 

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);

Ans: 1,2,3

 

7) Which of the following layout managers honours the preferred size of a component ?

1) CardLayout

2) FlowLayout

3) BorderLayout

4) GridLayout

Ans: 2

 

8) 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 ArrayIndexOutOfBoundsException

3) Print the values: 1, 2, 2, 4

4) Produces no output

Ans: 4

 

9) 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.

Ans: 2

 

10) 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);

Ans: 1

 

11) 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);

Ans: 1

 

12) 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

Ans: 4

 

13) Which of the following, are valid return types, for listener methods?

1) boolean

2) the type of event handled

3) void

4) Component

Ans: 3

 

14) 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(*);

Ans: 2

 

15) 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

Ans: 2

 

16) 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>

Ans: 2

 

17) Which of the following correctly illustrate how an InputStreamReader can be created ? (choose two)

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);

Ans: 1,5

 

18) 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

Ans: 2

 

19) 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

Ans: 3

 

20) 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

Ans: 3

 

21) What is the result of compiling and running the Second class?

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

}

}

 

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

Ans: 6

 

22) 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

Ans: 1

 

23) Consider the following classes. What is the result of compiling and running this class?

public class Test {

public static void test() {

this.print();

}

public static void print() {

System.out.println("Test");

}

public static void main(String args []) {

test();

}

}

 

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.

Ans: 6

 

24) 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.

Ans: 3

 

25) 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.

Ans: 4

 

26) Given the following sequence of Java statements, Which of the following options are true ?

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

 

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.

Ans: 4,6

 

27) 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();

}

}

 

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.

Ans: 6

 

28) Consider the following tags and attributes of tags, which can be used with the <AAPLET> and </APPLET> tags?

1. CODEBASE

2. ALT

3. NAME

4. CLASS

5. JAVAC

6. HORIZONTALSPACE

7. VERTICALSPACE

8. WIDTH

9. PARAM

10. JAR

 

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

Ans: 1,5

 

3S Technologies

Online Education for all