Sei sulla pagina 1di 4

CRECENCIA DRUSILA LOPEZ SENIOR HIGH SCHOOL

(SAN PABLO CITY SENIOR HIGH SCHOOL-STAND ALONE)


PROGRAMMING (JAVA)
DIAGNOSTIC TEST
NAME: ______________________________ SCORE: ______________
SECTION: ___________________________ AILEEN D. ENRIQUEZ

1. Which of the following is illegal?


A. int i = 32; A. public abstract void add();
B. float f = 45.0; B. public abstract void add() {}
C. double d = 45.0; C. public virtual add();
2. public class Test { D. public abstract add();
static int age; 10. Under what situations do you obtain a default constructor?
public static void main (String args []) {
age = age + 1;
A. When you define any class
System.out.println("The age is " + age);
B. When the class has no other constructors
}
C. When you define at least one constructor
}
11. Which of the following are acceptable to the Java compiler?

A. Compiles and runs with no output


A. if (2 == 3) System.out.println("Hi");
B. Compiles and runs printing out The age is 1
B. if (2 = 3) System.out.println("Hi");
C. Compiles but generates a runtime error
C. if (true) System.out.println("Hi");
D. Does not compile
D. if (2 != 3) System.out.println("Hi");
E. Compiles but generates a compile time error
E. if (aString.equals("hello")) System.out.println("Hi");
3. Which of the following are correct?
12. Assuming a method contains code which may raise an
Exception (but not a Runtime Exception), what is the correct
A. 128 >> 1 gives 64 way for a method to indicate that it expects the caller to handle
B. 128 >>> 1 gives 64 that exception:
C. 128 >> 1 gives -64
D. 128 >>> 1 gives -64
A. throw Exception
4. Which of the following return true?
B. throws Exception
C. new Exception
A. "john" == "john" D. Don't need to specify anything
B. "john".equals("john") 13. public void divide(int a, int b) {
C. "john" = "john" try {
D. "john".equals(new Button("john")) int c = a / b;
5. Which of the following are so called "short circuit" logical }
operators? catch (Exception e) {
System.out.print("Exception ");
A. & } finally {
B. || System.out.println("Finally");
C. && }
D. |
6. Which of the following are acceptable? A. Prints out: Finally
B. Prints out: Exception
A. Object o = new Button("A"); C. Prints out: Exception Finally
B. Frame f = new Panel();
C. Boolean flag = true; 1. No output
D. Boolean flag = true; 14. Which of the following statements is correct for a method which
E. Panel p = new Applet(); is overriding the following method:

7. public class Test { public void add(int a) {...}


static int total = 10;
public static void main (String args []) { A. the overriding method must return void
new Test();
B. the overriding method must return int
}
C. the overriding method can return whatever it likes
public Test () {
15. Given the following classes defined in separate files:
System.out.println("In test");
System.out.println(this); class Vehicle {
int temp = this.total; public void drive() {
if (temp > 5) { System.out.println("Vehicle: drive");
System.out.println(temp); }
}}} }

A. The compiler reports an error at line 2 class Car extends Vehicle {


B. The class will not compile public void drive() {
C. The value 10 is one of the elements printed to the System.out.println("Car: drive");
standard output }
D. The compiler reports an error at line 9 }
E. The class compiles but generates a runtime error
8. Which of the following is correct: public class Test {
public static void main (String args []) {
A. String temp [] = new String {"j" "a" "z"}; Vehicle v;
B. String temp [] = { "j " " b" "c"}; Car c;
C. String temp = {"a", "b", "c"}; v = new Vehicle();
D. String temp [] = {"a", "b", "c"}; c = new Car();
9. What is the correct declaration of an abstract method that is v.drive();
intended to be public: c.drive();
v = c;
v.drive(); A. sleep();
} B. stop();
} C. yield();
D. wait();
16. What will be the effect of compiling and running this class E. notify();
Test? 23. Which of the following methods are defined on the Graphics
class:
A. Generates a Compiler error on the statement v= c
B. Generates runtime error on the statement v= c A. drawLine(int, int, int, int)
C. Prints out: B. drawImage(Image, int, int, ImageObserver)
C. add(Component);
Vehicle : drive D. drawString(String, int, int)
Car : drive E. setVisible(boolean);
Car : drive 24. Which of the following layout managers honors the preferred
D. Prints out: size of a component:

Vehicle : drive A. CardLayout


Car : drive B. FlowLayout
Vehicle : drive C. BorderLayout
16. Where in a constructor, can you place a call to a constructor D. GridLayout
defined in the super class? 25. Given the following code what is the effect of a being 5:

A. The first statement in the constructor public class Test {


B. The last statement in the constructor public void add(int a) {
C. You can't call super in a constructor loop: for (int i = 1; i < 3; i++){
D. Anywhere for (int j = 1; j < 3; j++) {
17. Which variables can an inner class access from the class which if (a == 5) {
encapsulates it? break loop;
}
A. All static variables System.out.println(i * j);
B. All final variables }
C. All instance variables }
D. Only final instance variables }
E. Only final static variables }
18. What class must an inner class extend:
A. Generate a runtime error
A. The top level class B. Throw an ArrayIndexOutOfBoundsException
B. The Object class C. Print the values: 1, 2, 2, 4
C. Any class or interface D. Produces no output
D. It must extend an interface 26. What is the effect of issuing a wait() method on an object
19. In the following code, which is the earliest statement,
where the object originally held in e, may be garbage A. If a notify() method has already been sent to that object
collected: then it has no effect
B. The object issuing the call to wait() will halt until
1. public class Test { another object sends a notify() or notifyAll() method
2. public static void main (String args []) { C. An exception will be raised
3. Employee e = new Employee("Bob", 48); D. The object issuing the call to wait() will be automatically
4. e.calculatePay(); synchronized with any other objects using the receiving
5. System.out.println(e.printDetails()); object.
6. e = null; 27. The layout of a container can be altered using which of the
7. e = new Employee("Denise", 36); following methods:
8. e.calculatePay();
9. System.out.println(e.printDetails()); A. setLayout(aLayoutManager);
10. } B. addLayout(aLayoutManager);
11. } C. layout(aLayoutManager);
D. setLayoutManager(aLayoutManager);
A. Line 7 28. Using a FlowLayout manager, which is the correct way to add
B. Line 8 elements to a container:
C. Line 10
D. Line 11 A. set(component);
E. Never B. add("Center", component);
20. What is the name of the interface that can be used to define a C. add(x, y, component);
class that can execute within its own thread? D. add(component);
29. Given that a Button can generate an ActionEvent which listener
A. Run would you expect to have to implement, in a class which would
B. Runnable handle this event?
C. Thread
D. Threadable A. FocusListener
E. Executable B. WindowListener
21. What is the name of the method used to schedule a thread for C. ComponentListener
execution? D. ItemListener
E. ActionListener
A. init(); 30. Which of the following, are valid return types, for listener
B. start(); methods:
C. run();
D. resume(); A. boolean
E. sleep(); B. the type of event handled
22. Which methods may cause a thread to stop executing? C. Component
D. void
31. Assuming we have a class which implements the ActionListener D. Nothing happens
interface, which method should be used to register this with a 39. What is the result of executing the following Java class:
Button?
import java.awt.*;
A. addListener(*); public class FrameTest extends Frame {
B. addActionListener(*); public FrameTest() {
C. addButtonListener(*); add (new Button("First"));
D. setListener(*); add (new Button("Second"));
32. In order to cause the paint(Graphics) method to execute, which add (new Button("Third"));
of the following is the most appropriate method to call: pack();
setVisible(true);
}
A. paint()
B. repaint()
public static void main(String args []) {
C. paint(Graphics)
new FrameTest();
D. update(Graphics)
}
E. None, you should never cause paint(Graphics) to execute
}
33. Which of the following illustrates the correct way to pass a
parameter into an applet:
A. Three buttons are displayed across a window.
B. Only the "second" button is displayed.
A. <applet code=Test.class age=33 width=100 height=100>
C. Only the "third" button is displayed.
B. <param name=age value=33>
D. Only the "first" button is displayed.
C. <applet code=Test.class name=age value=33 width=100
E. A runtime exception is generated (no layout manager
height=100>
specified).
D. <applet Test 33>
40. Consider the following tags and attributes of tags:
34. Which of the following correctly illustrate how an
InputStreamReader can be created:
1. CODEBASE
2. ALT
A. new InputStreamReader(new FileInputStream("data"));
3. NAME
B. new InputStreamReader(new BufferedReader("data"));
4. CLASS
C. new InputStreamReader(System.in);
5. JAVAC
D. new InputStreamReader("data");
6. HORIZONTALSPACE
E. new InputStreamReader(new FileReader("data"));
7. VERTICALSPACE
35. What is the permanent effect on the file system of writing data to
8. WIDTH
a new FileWriter("report"), given the file report already exists?
9. PARAM
10. JAR
A. The data is appended to the file Which of the above can be used within the APPLET tags?
B. The file is replaced with a new file
C. An exception is raised as the file already exists
A. line 1, 2, 3
D. The data is written to random locations within the file
B. line 2, 5, 6, 7
36. What is the effect of adding the sixth element to a vector created
C. line 3, 4, 5
in the following manner:
D. line 8, 9, 10
E. line 8, 9
new Vector(5, 10);

A. An IndexOutOfBounds exception is raised.


B. The vector grows in size to a capacity of 10 elements
C. The vector grows in size to a capacity of 15 elements
D. Nothing, the vector will have grown when the fifth element
was added
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);

A. The value 3 is printed out


B. The values 3 and 4 are printed out
C. The values 1, 3 and 4 are printed out
D. Nothing is printed out
38. 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");
}
}

A. true is printed to standard out


B. false is printed to standard out
C. An exception is raised
PROGRAMMING (JAVA) 11. A/C/D/E 21. B 31. B
DIAGNOSTIC TEST 12. B 22. A 32. B
ANSWER KEYS 13. C 23. A/B/D 33. B
14. A 24. B 34. A/C
1. B 15. C/D 25. D 35. B
2. B 16. A 26. B 36. C
3. A/B 17. A/B/C 27. A 37. B
4. A/B 18. C 28. D 38. A
5. B/C 19. B 29. E 39. C
6. A/E 20. B 30. D 40. A/E
7. C
8. D
9. A
10. B

Potrebbero piacerti anche