Вы находитесь на странице: 1из 8

Java Questions

1. Study this code sample and select the correct option.

public class Base {


public void a1() throws NullPointerException {}
}

public class Derived extends Base {


public void a1() throws Exception {}
}

A. Both classes compile successfully


B. Base compiles, Derived does not compile
C. Derived compiles, Base does not
D. Derived throws NullPointerException at runtime

Answer : B

2. Study this code sample and select the correct option.

public class Base {


protected void a1() {}
}

public class Derived extends Base {


public void a1() {}
}

A. Both classes compile successfully


B. Base compiles, Derived does not compile
C. Derived compiles, Base does not
D. Throws java.lang.reflect.InvocationTargetException at runtime

Answer : A

3. Study this code sample and select the correct output.

class Animal {
void draw() {
System.out.print("Animal ");
}
}

class Cat extends Animal {


void draw() {
System.out.print("Cat ");
}
}

public class Animals {


public static void main(String[] args) {
Cat a1 = new Cat();
Animal a2 = new Animal();
Animal a3 = new Cat();
Animal a4 = (Animal)a1;

a1.draw();
a2.draw();
a3.draw();
a4.draw();
}
}

A. Cat Animal Cat Animal


B. Cat Animal Cat Cat
C. Class Animals does not compile
D. Throws ClassCastException

Answer : B

4. Study this code sample and select the correct output.

public class Foo


{
{
System.out.print("Initializer ");
}

static {
System.out.print("Static ");
}

Foo() {
System.out.print("Constructor ");
}

public static void main(String[] args)


{
System.out.print("Main ");
Foo foo = new Foo();
}
}

A. Static Main Initializer Constructor


B. Main Initializer Static Constructor
C. Initializer Static Main Constructor
D. Static Main Constructor Initializer

Answer : A

5. What would be the output of the following code segment?

public class Foo {


public void boo(String str) {
str = str + "World";
}

public static void main(String[] args) {


Foo foo = new Foo();
String str = new String("Hello ");
foo.boo(str);
System.out.println(str);
}
}

A. Hello
B. Hello World
C. World
D. Compilation Error
Answer : A

6. In the code segment below, what will be the value of the variable i at
completion?

i = 0;
j = 0;
for(j=1;j<10;j++) {
i = i+1;
}

A. 7
B. 8
C. 9
D. None of the above

Answer : C

7. When an untrusted application executes the code below, what happens if


the SecurityManager object is set up to deny this resource?

SecurityManager sm = System.getSecurityManager();
if(sm != null) {
sm.checkRead("filename.txt");
}

A. The SecurityManager will cause the Java Virtual Machine to exit.


B. The SecurityManager will terminate the thread attempting to read the file.
C. The SecurityManager will stop the read when read method calls are invoked.
D. checkRead() will throw a java.lang.SecurityException

Answer : D

8. For what value of X in the code below, will ALL the elements of the array
“values” be printed?
int values[] = {1,2,3,4,5,6,7,8};
for(int i=0;i< X;)
System.out.println(values[++i]);

A. 7
B. 8
C. 9
D. No value of X will print ALL the elements of the array.

Answer : D

9. What’s wrong with the following code segment?

while (x << 2) {
x++;
}

A. Loop expression is not a Boolean expression


B. ++x should be used instead of x++
C. There should be a “Do” at the end of the braces
D. ‘W’ should be capital in the “while”

Answer : A

10. What’s wrong with the following code segment?

for(int k=0; k<10; ++k) {


// If something;
break;
// else something;
continue;
}

A. Can’t have break and continue in the same for loop


B. Can’t use prefix operator in the for loop.
C. Can’t declare variable within the for loop definition
D. There’s nothing wrong with the code

Answer : D
Instructions : For questions 11 to 25, select the correct choice from the options
provided.

11. Consider a situation where two threads are sharing the same instance
variable. Each thread keeps its own, private copy of such a shared variable.
The real (or master) copy of the variable is updated at various times, such
as when a synchronized method is entered. To ensure that the master copy
reflects the current state at all times, which modifier would you use for the
shared variable?

A. Transient
B. Volatile
C. Static
D. Final

Answer : B

12. How many monitors (locks) are associated with an object?

A. One
B. Zero
C. As many as there are synchronized blocks
D. As many as there are threads accessing the object

Answer : A

13. Which of the following thread state transitions is invalid?

A. From ready to running


B. From ready to waiting
C. From running to ready
D. From running to waiting

Answer : B

14. What is the default port for the RMI registry?

A. 389
B. 9080
C. 1099
D. None of the above

Answer : C

15. What is a Singleton class?

A. The class is loaded only once by the JVM


B. There is only one instance of the class in the JVM
C. There is only one instance of the class per classsloader
D. The class is loaded only once by each classloader

Answer : C
16. The Java platform security imposes restrictions on applets without
explicit permissions to do which of the following?

A. Display images
B. Accept user input
C. Write to system files
D. Initialize data in the browser

Answer : C

17. Which of the following statements is true?

A. Window extends Frame


B. Applet extends Window
C. Panel extends Container
D. ScrollPane extends Panel

Answer : C

18. Which of the following methods is called by AWT to support repaint


operations?

A. paint()
B. repaint()
C. draw()
D. update()

Answer : A

19. When two or more objects are added as listeners of the same event,
which listener is invoked first?

A. The first object that was added as a listener


B. The last object that was added as a listener
C. Order is undefined.
D. It is impossible to have more than one listener for an event.

Answer : C

20. How can you force an object to be garbage collected?

A. Invoke it’s finalize() method


B. Remove all references to the object
C. Use all memory that’s available to the program
D. You can not force an object to be garbage collected

Answer : D

21. Which of the following is the highest class in the event delegation class
hierarchy?

A. java.util.EventListener
B. java.util.EventObject
C. java.awt.AWTEvent
D. java.awt.event.AWTEvent

Answer : B

22. System.err represents an instance of which class?

A. java.lang.System
B. java.io.PrintWriter
C. java.io.OutputStreamWriter
D. java.io.PrintStream

Answer : D

23. Which of the following are true about the File class?

A. File object can be used to access the files and directories on the local file
system
B. When a File object is created, a corresponding file or a directory is created in
the local file system.
C. When a File object is garbage collected the corresponding file or directory in
the file system is deleted.
D. All of the above

Answer : A

24. Which of the following are true about the garbage collector in java?

1. It executes as a low-priority background thread


2. It guarantees that a java program never runs out of memory
3. It keeps track of which objects are reachable and unreachable
4. It can be directed to garbage collect specific objects

A. 1 and 2
B. 2 and 3
C. 2, 3 and 4
D. 1 and 3

Answer : D

25. Study the following options. Which of these are true?

1. Hashtable is synchronized, HashMap is not


2. Hashtable does not allow non-null values, HashMap does
3. Hashtable does not implement Serializable, HashMap does
4. Hashtable does not implement Map interface, HashMap does

A. 1 and 2 only
B. 1, 2 and 3
C. 1 and 3 only
D. 1 and 4 only

Answer : A
Instructions : For questions 26 to 30, select whether the given statement is true or
false.

27. A top-level class can not be declared as static.

A. True
B. False

Answer : A

28. An interface can have protected members.

A. True
B. False

Answer : B

29. The List interface implementation permits duplicate elements in a


collection object.

A. True
B. False

Answer : A

30. An object of class java.util.ArrayList can be used to store objects of any


type.

A. True
B. False

Answer: A

Вам также может понравиться