Java-Programitical questions, logical questions,technical questions part-I

This paper gives an idea about java coding questions for freshers which are helpful for interview exams…

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
Answer 3

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
Answer : 3

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
Answer : 1

What is the name of the method used to schedule a thread for execution?
1) init();
2) start();
3) run();
4) resume();
5) sleep();
Answer : 2

Which methods may cause a thread to stop executing?
(multiple)
1) sleep();
2) stop();
3) yield();
4) wait();
5) notify();
6) notifyAll()
7) synchronized()
Answer : 1,2,3,4

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

Answer : 1

Which of the following methods are defined on the Graphics class:
(multiple)

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);
Answer : 1,2,3

Which of the following layout managers honours the preferred size of a component:
(multiple)
1) CardLayout
2) FlowLayout
3) BorderLayout
4) GridLayout
Answer : 2

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
Answer : 4

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.
Answer : 2

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

(multiple)
1) setLayout(aLayoutManager);
2) addLayout(aLayoutManager);
3) layout(aLayoutManager);
4) setLayoutManager(aLayoutManager);
Answer : 1

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);
Answer : 1

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
Answer : 4

Which of the following, are valid return types, for listener methods:
1) boolean
2) the type of event handled
3) void
4) Component
Answer : 3

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(*);
Answer : 2

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
Answer : 2

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>

Answer : 2

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

(multiple)
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);
Answer : 1,5

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
Answer : 2

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
Answer : 3

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
Answer : 3

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
Answer : 6

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
Answer : 1

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

(multiple)
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.
Answer : 6

If you have questions, please ask below

One thought on “Java-Programitical questions, logical questions,technical questions part-I

  1. Tauseef

    This was the excellent and satisfactory information plz send more MCQ’s like this on my email id.
    Thank you ….

    Reply

Leave a Reply