Download Mock Exam [2]

Document related concepts

Java ConcurrentMap wikipedia , lookup

Transcript
Mock Exam [2]
Q1
Here is part of the code for a class which implements the Runnable interface.
1. public class Whiffler extends Object implements Runnable {
2. Thread myT ;
3. public void start(){
4. myT = new Thread( this );
5. }
6. public void run(){
7. while( true ){
8. doStuff();
9. }
10. System.out.println("Exiting run");
11. }
12. // more class code
Assume that the rest of the class defines doStuff, etc and that the class compiles without error. Also assume
that a Java application creates a Whiffler object and calls the Whiffler start method, that no other direct calls
to Whiffler methods are made an that the Thread in this object is the only one the application creates. Which
of the following are correct statements ?
a. The doStuff method will be called repeatedly.
b. The doStuff method will never be executed.
c. The doStuff method will execute at least one time.
d. The statement in line 10 will never be reached.
Q2
Here is a method which creates a number of String objects in the course of printing a count down sequence.
1. public void countDown() {
2. for( int i = 10 ; i >= 0 ; i-- ){
3. String tmp = Integer.toString( i );
4. System.out.println( tmp );
5. }
6. System.out.println("BOOM!");
7. }
When the program reaches line 6, how many of the String objects created in line 3 are eligible for garbage
collection? Assume that the System.out object is not keeping a reference.
a. none
b. 1
c. 10
d. 11
Q3
Select all of the following methods that are instance methods of the Thread class, excluding any methods deprecated
in Java 1.2.
a. start()
b. stop()
c. run()
d. suspend()
e. sleep( long msec )
f. toString()
Q4
You are writing a set of classes related to cooking and have created your own exception hierarchy derived from
java.lang.Exception as follows
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your base class, "BaseCook" has a method declared as follows
int rateFlavor(Ingredient[] list) throws BadTasteException
A class, "TexMexCook", derived from BaseCook has a method which overrides BaseCook.rateFlavor(). Which of the
following are legal declarations of the overriding method?
a. int rateFlavor(Ingredient[] list) throws BadTasteException
b. int rateFlavor(Ingredient[] list) throws Exception
c. int rateFlavor(Ingredient[] list) throws BitterException
d. int rateFlavor(Ingredient[] list)
Q5
You are working on an aquarium simulation class named Aquarius. You already have a method which adds a Fish object
to the aquarium and returns the remaining fish capacity. This method has the following declaration.
public int addFish( Fish f )
Now you want to provide for adding a whole shoal of fish at once. The proposed method declaration is
protected boolean addFish( Fish[] f )
The idea being that it will return true if there is more room in the tank or false if the tank now too full.
Which of the following statements about this proposal are true?
a. This technique is called overloading.
b. This technique is called overriding.
c. The compiler will reject the new method because the return type is different.
d. The compiler will reject the new method because the access modifier is different.
Q6
You have created a TimeOut class as an extension of Thread, the purpose being to print a "Time's Up" message
if the Thread is not interrupted within 10 seconds of being started.
Here is the run method which you have coded.
1. public void run(){
2. System.out.println("Start!");
3. try {
4. Thread.sleep(10000 );
5. System.out.println("Time's Up!");
6. } catch(InterruptedException e) {
7. System.out.println("Interrupted!");
8. }
9. }
Given that a program creates and starts a TimeOut object, which of the following statements is true?
a. Exactly 10 seconds after the start method is called, "Time's Up!" will be printed.
b. Exactly 10 seconds after "Start!" is printed, "Time's Up!" will be printed.
c. The delay between "Start!" being printed and "Time's Up!" will be 10 seconds plus or minus one tick of the
system clock.
d. If "Time's Up!" is printed you can be sure at least 10 seconds have elapsed since "Start!" was printed.
Q7
What is the output of the following program?
1. class Base {
2. int x=3;
3. public Base() {}
4. public void show() {
5. System.out.print(" The value is " + x);
6. }
7. }
8. class Derived extends Base {
9. int x=2;
10. public Derived() {}
11. public void show() {
12. System.out.print(" The value is " + x);
13. }
14. }
15. public class Test {
16. public static void main(String args[]) {
17. Base b = new Derived();
18. b.show();
19. System.out.println("The value is " +b.x);
20. }
21. } // end of class Test
a. The value is 3 The value is 2
b. The value is 2 The value is 3
c. The value is 3 The value is 3
d.The value is 2 The value is 2
Q8
What is the output of the following code?
1. public class Note {
2. public static void main(String args[]) {
3. String name[] = {"Killer","Miller"};
4. String name0 = "Killer";
5. String name1 = "Miller";
6. swap(name0,name1);
7. System.out.println(name0 + "," + name1);
8. swap(name);
9. System.out.println(name[0] + "," + name[1]);
10. }
11. public static void swap(String name[]) {
12. String temp;
13. temp=name[0];
14. name[0]=name[1];
15. name[1]=temp;
16. }
17. public static void swap(String name0,String name1) {
18. String temp;
19. temp=name0;
20. name0=name1;
21. name1=temp;
22. }
23. } // end of Class Note
a. Killer,Miller followed by Killer,Miller
b. Miller,Killer followed by Killer,Miller
c. Killer,Miller followed by Miller,Killer
d. Miller,Killer followed by Miller,Killer
Q9
What is the output of the following program?
1. public class Q11 {
2. static String str1 = "main method with String[] args";
3. static String str2 = "main method with int[] args";
5. public static void main(String[] args) {
6. System.out.println(str1);
7. }
10. public static void main(int[] args) {
11. System.out.println(str2);
12. }
13. }
a. Duplicate method main(), compilation error at line 5.
b. Duplicate method main(), compilation error at line 10.
c. Prints "main method with String[] args".
d. Prints "main method with int[] args".
Q10
In the following applet, how many buttons will be displayed?
1. import java.applet.*;
2. import java.awt.*;
3. public class Q16 extends Applet {
4. Button okButton = new Button("Ok");
5. public void init() {
6. add(okButton);
7. add(okButton);
8. add(okButton);
9. add(okButton);
10. add(new Button("Cancel"));
14. add(new Button("Cancel"));
15. add(new Button("Cancel"));
16. add(new Button("Cancel"));
18. setSize(300,300);
19. }
20. }
a. 1 Button with label "Ok" and 1 Button with label "Cancel".
b. 1 Button with label "Ok" and 4 Buttons with label "Cancel".
c. 4 Buttons with label "Ok" and 1 Button with label "Cancel".
d. 4 Buttons with label "Ok" and 4 Buttons with label "Cancel".
Q11
What is the output of the following program?
1. class Test {
2. void show() {
3. System.out.println("non-static method in Test");
4. }
5. }
6. public class Q3 extends Test {
7. static void show() {
8. System.out.println("Overridden non-static method in Q3");
9. }
10. public static void main(String[] args) {
11. Q3 a = new Q3();
12. }
13 }
a. Compilation error at line 2.
b. Compilation error at line 7.
c. No compilation error, but runtime exception at line 2.
d. No compilation error, but runtime exception at line 7.
Q12
What will happen if you compile/run the following code?
1. public class Q21 {
2. int maxElements;
3. void Q21() {
4. maxElements = 100;
5. System.out.println(maxElements);
6. }
7. Q21(int i) {
8. maxElements = i;
9. System.out.println(maxElements);
10. }
11. public static void main(String[] args) {
12. Q21 a = new Q21();
13. Q21 b = new Q21(999);
14.
15. }
16. }
a. Prints 100 and 999.
b. Prints 999 and 100.
c. Compilation error at line 2, variable maxElements was not initialized.
d. Compilation error at line 12.
Q13
Given the following class definition, which of the following methods could be legally placed after the comment
//Here
1. public class Rid {
2. public void amethod(int i, String s){}
3. //Here
4. }
a. public void amethod(String s, int i) {}
b. public int amethod(int i, String s) {}
c. public void amethod(int i, String mystring) {}
d. public void Amethod(int i, String s) {}
Q14
Given the following class definition, which of the following statements would be legal after the comment //Here
1. class InOut {
2. String s= new String("Between");
3. public void amethod(final int iArgs) {
4. int iam;
5. class Bicycle{
6. public void sayHello(){
7. //Here
8. } //End of bicycle class
9. }
10. } //End of amethod
11. public void another(){
12. int iOther;
13. }
14. }
a. System.out.println(s);
b. System.out.println(iOther);
c. System.out.println(iam);
d. System.out.println(iArgs);
Q15
Which of the following methods are members of the Vector class and allow you to input a new element
a. addElement()
b. insert()
c. append()
d. addItem()
Q16
What will happen when we try to compile the following code.
1. public void WhichArray( Object x ) {
2. if( x instanceof int[] ) {
3. int[] n = (int[]) x ;
4. for( int i = 0 ; i < n.length ; i++ ){
5. System.out.println("integers = " + n[i] );
6. }
7. }
8. if( x instanceof String[] ) {
9. System.out.println("Array of Strings");
10. }
11. }
a. The compiler objects to line 2 comparing an Object with an array.
b. The compiler objects to line 3 casting an Object to an array of int primitives.
c. The compiler objects to line 7 comparing an Object to an array of Objects.
d. It compiles without error.
Q17
Consider the following class
1. class Tester {
2. void test (int i) { System.out.println ("int version"); }
3. void test (String s) { System.out.println ("String version"); }
4.
5. public static void main (String args[]) {
6. Tester c = new Tester ();
7. char ch = 'p';
8. c.test (ch);
9. }
10. }
Which of the following statements below is true?(Choose one.)
a. Line 3 will not compile, because void methods cannot be overridden.
b. Line 8 will not compile, because there is no conversion of test() that takes a char argument.
c. The code will compile and produce the follwing output "int version"
d. The code will compile and produce the follwing output "String version"
Q18
You are creating a ToolBase class which will be extended by other programmers. The ToolBase class contains a
single abstract method, createTool.
Which of the following statements are true?
a. The ToolBase class must be declared abstract.
b. Classes extending ToolBase must not be declared abstract.
c. The ToolBase class must not be declared final.
d. The following variable declaration is illegal in any context "ToolBase myTB;"
Q19
Given the following hierarchical relationship of several classes.
1. Object
2. |---TypeA
3. | |-----TypeAB
4. | |-----TypeAC
5. |--------TypeY
And given the following method definition
6. public sayType(Object x ){
7. if(x instanceof Object )System.out.print("Object,");
8. if(x instanceof TypeA )System.out.print("TypeA,");
9. if(x instanceof TypeAB )System.out.print("TypeAB,");
10. if(x instanceof TypeAC )System.out.print("TypeAC,");
11. }
What would the program output be if the following line was executed
12. sayType( new TypeAB() );
a. Object,
b. Object,TypeA,TypeAB,
c. TypeAB,
d. Object,TypeAC,
Q20
What happens on trying to compile and run the following code?
1. public class EqualsTest{
2. public static void main(String args[]){
3. Long LA = new Long( 9 ) ;
4. Long LB = new Long( 9 ) ;
5. if( LA == LB ) System.out.println("Equal");
6. else System.out.println("Not Equal");
7. }
8. }
a. The program compiles but throws a runtime exception in line 5.
b. The program compiles and prints "Not Equal".
c. The program compiles and prints "Equal".
d. The program throws compilation error.
Q21
Which one statement in true about the application below?
1. class StaticStuff {
2. static int x = 10;
3. static { x += 5; }
4. public static void main(String args[]) {
5. System.out.println("x = " + x);
6. }
7. static { x /= 5; }
8. }
a. The code compiles, and execution produces the output x = 10.
b. The code compiles, and execution produces the output x = 15.
c. The code compiles, and execution produces the output x = 3.
d. Line 7 will not compile, because you can have only one static initializer.
Q22
Assume the the class AcLis implements the ActionListener interface. The code fragment below constructs a button
ande gives it four action listeners. When the button is pressed, which action listener is the first to get its
actionPerformed() method invoked?
1. Button btn = new Button("Hello");
2. AcLis a1 = new AcLis();
3. AcLis a2 = new AcLis();
4. AcLis a3 = new AcLis();
5. btn.addActionListener(a1);
6. btn.addActionListener(a2);
7. btn.addActionListener(a3);
8. btn.removeActionListener(a2);
9. btn.removeActionListener(a3);
10. btn.addActionListener(a3);
11. btn.addActionListener(a2);
a. a1 gets its actionPerformed() method invoked first.
b. a2 gets its actionPerformed() method invoked first.
c. a3 gets its actionPerformed() method invoked first.
d. It is impossible to know which listener will be first.
Q23
Which statement or statements are true about the code listed below?
1. public class MyTextArea extends TextArea {
2. public MyTextArea(int nrows, int ncols) {
3. enableEvents(AWTEvent.TEXT_EVENT_MASK);
4. }
5. public void processTextEvent(TextEvent te) {
6. System.out.println("Processing a text event");
7. }
8. }
a. The source code must appear in a file called MyTextArea.java.
b. Between lines 2 and 3, a call should be made to super(nrows,ncols) so that the next component will have the
correct size.
c. Between lines 6 and 7, the following code should appear "return true;"
d. Between lines 6 and 7, the following code should appear "super.processTextEvent(te);"
Q24
Which statement or statements are true about the code fragment listed below? (HINT The ActionListener and
ItemListener interface each define a single method.)
1. class MyListener implements ActionListener, ItemListener {
2. public void actionPerformed(ActionEvent e) {
3. System.out.println("Action.");
4. }
5. public void itemStateChanged(ItemEvent ie) {
6. System.out.println("Item.");
7. }
8. }
a. The code compiles without error.
b. The code generates a compiler error at line 1.
c. The code generates a compiler error at line 2.
d. The code generates a compiler error at line 5.
Q25
A text field has a variable-width font. It is constructed by calling new TextField("iiiii"). What happens if
you change the contents of the textfield to "wwwww"? (NOTE i is one of the narrowest characters, and w is one
of the widest.)
a. The text field becomes wider.
b. The text field becomes narrower.
c. The text field stays the same width; to see the entire contents you have to scroll using -> and <-.
d. The text field stays the same width; to see you need to have a horizontal scroll bar.
Q26
Which statements about garbage collection are true? Select all valid answers
a. You can directly free the memory allocated by an object.
b. You can directly run the garbage collector whenever you want.
c. The garbage collector informs your object when it is about to be garbage collected.
d. The garbage collector runs in low-memory situations.
Q27
The setForeground() and setBackground() methods are defined in the following class
a. Graphics
b. Container
c. Component
d. Applet
Q28
What methods does JAVA define in the java.lang.Math class specifically for trigonometric calculations? Select
all valid answers.
a. cos()
b. asin()
c. arctan()
d. sine()
Q29
Imagine that there are two exception classes called Exception1 and Exception2 that descend from the Exception
class. Given these two class definitions,
1. class First {
2. void test() throws Exception1, Exception2 { . . . }
3. }
4. class Second extends First {
5. void test() { . . . }
Create a class called Third that extends Second and defines a test() method. What exceptions can Third's test
method throw? Select all valid answers.
a. Exception1
b. Exception2
c. no checked exceptions
d. any exceptions declared in the throws clause of the Third's test() method.
Q30
Given these code snippets,
1. Boolean b1 = new Boolean(true);
2. Boolean b2 = new Boolean(true);
which expressions are legal JAVA expressions that return true? Select all valid answers.
a. b1 == b2
b. b1.equals(b2)
c. b1 & b2
d. b1 || b2
Q31
What will appear in the standard output when you run the Tester class?
1. class Tester {
2. int var;
3. Tester(double var) {
4. this.var = (int)var;
5. }
6. Tester(int var) {
7. this("hello");
8. }
9. Tester(String s) {
10. this();
11. System.out.println(s);
12. }
13. Tester() {
14. System.out.println("good-bye");
15. }
16. public static void main(String args[]) {
17. Tester t = new Tester(5);
18. }
a. "hello"
b. 5
c. "hello" followed by "good-bye"
d. "good-bye" followed by "hello"
Q32
What are the range of values for a variable of type short?
a. -2^7 to 2^7-1
b. 0 to 2^8
c. -2^15 to 2^15-1
d. -2^15-1 to 2^15
Q33
Analyze the following two classes
1. class First {
2. static int a = 3;
3. }
4. final class Second extends First {
5. void method() {
6. System.out.println(a);
7. }
8. }
a. Class First compiles, but class Second does not.
b. Neither class compiles.
c. Both classes compile, and if method() is invoked, it writes 3 to the output.
d. Both classes compile, and if method() is invoked, it throws an exception.
Q34
What String instance method would return true when invoked as follows?
1. a.method(b);
where a equals "GROUNDhog" and b equals "groundHOG"?
a. equals()
b. toLowerCase()
c. toUpperCase()
d. equalsIgnoreCase()
Q35
What access control keyword should you use to enable other classes to access a method freely within its package,
but to restrict classes outside of the package from accessing that method? Select all valid answers.
a. private
b. public
c. protected
d. Do not supply an access control keyword (friendly).
Q36
What letters are written to the standard output with the following code?
1. class Unchecked {
2. public static void main(String args[]) {
3. try {
4. method();
5. } catch(Exception e) { }
6. }
7. static void method() {
8. try {
9. wrench();
10. System.out.println("a");
11. } catch(ArithmeticException e) {
12. System.out.println("b");
13. } finally {
14. System.out.println("c");
15. }
16. System.out.println("d");
17. }
18. static void wrench() {
19. throw new NullPointerException();
20. }
21. }
Select all valid answers.
a. "a"
b. "b"
c. "c"
d. "d"
Q37
If you would like to change the size of a Component, you can use the following method
a. size()
b. resize()
c. setSize()
d. dimension()
Q38
Which LayoutManager arranges components left to right and then top to bottom, centering each row as it moves
to the next row?
a. BorderLayout
b. FlowLayout
c. GridLayout
d. CardLayout
Q39
Which label names(s) are illegal? Select all valid answers.
a. here
b. _there
c. this
d. 2to1odds
Q40
Which keyword, when used in front of a method, must also appear in front of the class.
a. synchronized
b. abstract
c. public
d. private
Q41
Given this code snippet,
1. try {
2. tryThis();
3. return;
4. } catch(IOException x1) {
5. System.out.println("exception 1");
6. return;
7. } catch(Exception x2) {
8. System.out.println("exception 2");
9. return;
10. } finally {
11. System.out.println("finally");
12. }
what will appear in the standard output if tryThis() throws a IOException?
a. "exception 1" followed by "finally"
b. "exception 2" followed by "finally"
c. "exception 1"
d. "exception 2"
Q42
Given the code snippet,
1. double a = 90.7;
2. double b = method(a);
3. System.out.println(b);
if this snippet displays 90.0 in the standard output, what Math method did method() invoke? Select all valid
answers.
a. abs()
b. round()
c. floor()
d. ceil()
Q43
What is written to the standard output given the following statement
1. System.out.println(4 | 7);
a. 4
b. 5
c. 6
d. 7
Q44
What expressions are true concerning the following lines of code?
1. int[] arr = {1, 2, 3};
2. for(int i = 0 ; i < 2; i++)
3. arr[i] = 0;
Select all valid answers.
a. arr[0] == 0
b. arr[1] == 2
c. arr[1] == 0
d. arr[2] == 3
Q45
What will the following block of code write to standard output when it is executed?
1. int i = 3;
2. int j = 0;
3. double k = 3.2;
4. if (i < k)
5. if (i == j)
6. System.out.println(i);
7. else
8. System.out.println(j);
9. else
10. System.out.println(k);
a. 3
b. 0
c. 3.2
d. None of these.
Q46
What is the output of the following piece of code
1. int x = 6;
2. double d = 7.7;
3. System.out.println((x>d) ? 99.9 : 9);
a. 9
b. 9.0
c. 99.9
d. Nothing, an ArithmeticException is thrown at line 3.
Q47
Which of the following are legal methods for the String class ?
a. length()
b. toUpper()
c. toUppercase()
d. equals()
Q48
Consider the following
1. class A extends Integer {
2. int x = 0;
3. }
a. The code will compile correctly.
b. The code will not compile because Integer class is final.
c. The code will not compile because A class doesn't have a constructor.
d. The code will compile but an ArithmeticException at runtime.
Q49
FlowLayout is the default layout manager for which of the following containers. Select all valid answers?
a. Panel
b. Applet
c. Frame
d. Dialog
Q50
Using a FlowLayout Manager, which of the following is the correct way to add a component reference by the variable
"c" to a container.
a. add(c)
b. c.add()
c. add("Center",c)
d. set(c)
Q51
Given the following code snippet
1. class A {
2. public void method(int a, float b) {
3. // some declaration and etc.
4. }
5. }
6. public class B extends A {
7. //Comment here ?
8. }
In class B what all methods can be placed in (// Comment here ?) individually ?
a. void method(int i, float a)
b. public void method(int i, float f)
c public void method()
d. protected int method(float f, int b)
Q52
What does the following program do when it is run with the following command?
java Mystery Mighty Mouse
1. class Mystery {
2. public static void main(String args[]) {
3. Changer c = new Changer();
4. c.method(args);
5. System.out.println(args[0] + " " + args[1]);
6. }
7. static class Changer {
8. void method(String s[]) {
9. String temp = s[0];
10. s[0] = s[1];
11. s[1] = temp;
12. }
13. }
14. }
a. The program causes and ArrayIndexOutOfBoundsException to be thrown.
b. The program runs but does not write anything to the standard output.
c. The program writes "Mighty Mouse" to the standard output.
d. The program writes "Mouse Mighty" to the standard output.
Q53
What happens when you try to compile and run the following program?
1. class Mystery {
2. String s;
3. public static void main(String args[]) {
4. Mystery m = new Mystery();
5. m.go();
6. }
7. void Mystery() {
8. s = "Constructor";
9. }
10. void go() {
11. System.out.println(s);
12. }
13. }
a. The code compiles and throws an exception at run time.
b. The code runs, but nothing appears in the standard output.
c. The code runs, and "constructor" appears in the standard output.
d. The code runs and writes "null" in the standard output.
Q54
What can you write at the comment //A in the following code so that this program writes the word "running" to
the standard output?
1. class RunTest implements Runnable {
2. public static void main(String args[]) {
3. RunTest rt = new RunTest();
4. Thread t = new Thread(rt);
5. //A
6. }
7. public void run() {
8. System.out.println("running");
9. }
10. void go() {
11. start(1);
12. }
13. void start(int i) {
14. }
15. }
Select all valid answers.
a. System.out.println("running");
b. t.start();
c. rt.start();
d. rt.start(1);
Q55
What is wrong with the following code?
1. final class First {
2. private int a = 1;
3. int b = 2;
4. }
5. class Second extends First {
6. public void method() {
7. System.out.println(a+b);
8. }
9. }
a. You cannot invoke println() without passing it a string.
b. Because a is private, no classes other than First can access it.
c. Second cannot extend First.
d. final is not a valid keyword for a class.
Q56
What is displayed when the following piece of code is executed.
1. class Test extends Thread {
2. public void run() {
3. System.out.print("1 ");
4. yield();
5. System.out.print("2 ");
6. suspend();
7. System.out.print("3 ");
8. resume();
9. System.out.print("4 ");
10. }
11 public static void main(String []args) {
12. Test t = new Test();
13. t.start();
14. }
15. }
a. Nothing, this is not a valid way to create and start a thread.
b. 1 2
c. 1 2 3
d. 1
Q57
What must be true for the RunHandler class so that the instances of RunHandler can be used as written in the
following code?
1. class Test {
2. public static void main(String args[]) {
3. Thread t = new Thread(new RunHandler());
4. t.start();
5. }
6. }
Select all valid answers ?
a. RunHandler must only implement the java.lang.Runnable interface.
b. RunHandler must only extend the Thread class.
c. RunHandler must extend the Thread class or implement Runnable interface.
d. RunHandler must provide a run() method declared as public and returning void.
Q58
Which interface implementations can you add as listeners for a TextField object? Select all valid answers.
a. ActionListener
b. FocusListener
c. MouseMotionListener
d. ContainerListener
Q59
Which statements accurately describe the following line of code?
String s[][] = new String[10][];
Select all valid answers.
a. This line of code is illegal.
b. s is a two dimensional array containing 10 rows and 10 columns.
c. s is an array of 10 arrays.
d. Each element in s is set to "".
Q60
To force a layout manager to re-layout the components in a container, you can invoke the container method called
a. validate()
b. repaint()
c. layout()
d. update();
Q61
You need a container to hold six equal-sized button in the three columns of two. This arrangement must persist
when the container is resized. Which of the following will create the that container?
a. Canvas c = new Canvas(new GridLayout(2,3));
b. Panel p = new Panel(new GridLayout(2,3));
c. Window w = new Window(new GridLayout(2,3));
d. Panel p = new Panel(new GridLayout(3,2));
Q62
The following lists the complete contents of the file named Derived.java
1. public class Base extends Object {
2. String objType;
3. public Base() { objType = "I am a Base type" ; }
4. }
5. public class Derived extends Base {
6. public Dervied() { objType = "I am a Dervied type"; }
8. public static void main(String args[]) {
9. Dervied d = new Derived();
10. }
11. }
What will happen when this file is compiled?
a. Two class files,Base.class and Dervied.class, will be created.
b. The compiler will object to line 1.
d. The compiler will object to line 2.
c. The compiler will object to line 5.
Q63
Your mouseDragged() event handler and your paint method look like this
1. public void mouseDragged(MouseEvent e) {
2. mouseX = e.getX();
3. mouseY = e.getY();
4. repaint();
5. }
6. public void paint(Graphics g) {
7. g.setColor(Color.cyan);
8. g.drawLine(mouseX,mouseY,mouseX+10,mouseY+10);
9. }
You want to modify your code so that the cyan lines accumulate on the screen, rather than getting erased every
time repaint() calls update(). What is the simplest way to proceed?
a. On line 4, replace repaint() with paint().
b. On line 4, replace repaint() with update().
c. After line 7, add this super.update(g);
d. Add public void update(Graphics g) { paint(g); }
Q64
Given the following code for the Demo class
1. public class Demo {
2. private String [] userNames;
3. public Demo() {
4. userNames = new String[10];
5. }
6. public void showName(int n) {
7. System.out.println("Name is " + userNames[n]);
8. }
9.
10. public String getName(int n) {
11. return(userNames[n]);
12. }
13. }
What would be the result of calling the showName method with a parameter of 2 immediately after creating an instance
of Demo
a. Standard output would show "Name is null".
b. A NullPointerException would be thrown, halting the program.
c. An ArrayIndexOutOfBoundsException would be thrown halting the program.
d. Standard output would show "Name is".
Q65
What will be the result of attempting to compile and run the following class?
1. public class Integers {
2. public static void main(String args[]) {
3. System.out.printl(0x10 + 10 + 010);
4. }
5. }
Select the one right answer.
a. The code won't compile. The compiler will complain about the expression 0x10 + 10 + 010
b. When run, the program will print "28"
c. When run, the program will print "34"
d. When run, the program will print "36"
Q66
How will the following program lay out its buttons?
1. import java.awt.*;
2. public class MyClass {
3. public static void main(String args[]) {
4. String labels[] = {"A","B","C","D","E","F"};
5. Window grid = new Frame();
6. grid.setLayout(new GridLayout(0,1,2,3));
7. for(int i=0;i 8. grid.add(new Button(labels[i]));
9. }
10. grid.pack();
11. grid.setVisible(true);
12. }
Select the one right answer
a. The program will not show any buttons at all.
b. It will place all buttons in one row - A B C D E F.
c. It will place all buttons in one column, i.e., each button in a separate row - A B C D E and F.
d. It will place pairs of buttons in three separate row A B, C D and E F.
Q67
What happens when this method is called with an input of "Java rules"?
1. public String addOK(String S) {
2. S += " OK!";
3. return S;
4. }
Select one correct answer
a. The method will return "OK!";
b. A runtime exception will be thrown.
c. The method will return "Java rules OK!".
d. The method will return "Java rules".
Q68
What happens during execution of the following program?
1. public class OperandOrder {
2. public static void main(String args[]) {
3. int i=0;
4. int a[] = {3,6};
5. a[i] = i = 9;
6. System.out.println(i + " " + a[0] + " " + a[1]);
7. }
8. }
Select one correct answer.
a. Raises "ArrayIndexOutOfBoundsException".
b. Prints "9 9 6".
c. Prints "9 0 6".
d. Prints "9 3 6".
Q69
Which statements about the output of the following program are true?
1. public class Logic {
2. public static void main(String args[]) {
3. int i = 0;
4. int j = 0;
5. boolean t = true;
6. boolean r;
7. r = (t & 0<(i += 1));
8. r = (t && 0<(i += 2));
9. r = (t | 0<(j += 1));
10. r = (t || 0<(j += 2));
11. System.out.println(i + " " + j);
12. }
13. }
Select all the valid answers.
a. The first digit printed is 1.
b. The first digit printed is 3.
c. The second digit printed is 3.
d. The second digit printed is 1.
Q70
What kind of stream is the System.out object?
a. java.io.BufferedWriter
b. java.io.PrintStream
c. java.io.FileWriter
d. java.io.OutputStreamWriter
Q71
Which of the following events has a matching adapter class that implements the appropriate listener interface?
Select all correct answers.
a. java.awt.event.MouseEvent
b. java.awt.event.ActionEvent
c. java.awt.event.FocusEvent
d. java.awt.event.ItemEvent
Q72
Here is a partial listing of a class to represent a game board in a networked game. It is desired to prevent
collisions due to more than one Thread using the addPic method to modify the array of Image references.
1. class Board extends Object {
2. Image[] pics = new Image[64];
3. int active = 0;
4. public boolean addPic( Image mg, int pos) {
5. synchronized (this) {
6. if (pics[pos] == null)
7. { active++ ; pics[pos] = mg; return true; }
8. else
9. return false;
10. }
11. }
12. // remainder of class
Select all the alternatives for line 5 that would accomplish this.
a. synchronized (this)
b. synchronized (pics)
c. synchronized (mg)
d. synchronized (active)
Q73
Given an object created by the following class
1. class Example extends Object {
2. public void Increment (Integer N) {
3. N = new Integer( N.intValue() + 1);
4. }
6. public void Result(int x) {
7. Integer X = new Integer(x);
8. Increment(X);
9. System.out.println("New value is" + X);
10. }
11. }
What happens when a program calls the Result method with a value of 30?
a. The message "New value is 31" goest to the standard output.
b. The message "New value is 29" goes to the standard output.
c. The message "New Value is 30" goes to the standard output.
d. The program fails to compile.
Q74
What is the output of the following program
1. public class Test {
2. private int i = giveMeJ();
3. private int j = 10;
4.
5. private int giveMeJ() {
6. return j;
7. }
8.
9. public static void main(String args[]) {
10. System.out.println((new AQuestion()).i);
11. }
12. }
Select one correct answer
a. Compiler error complaining about access restriction of private variables of AQuestion.
b. Compiler error complaining about forward referencing.
c. No Compilation error - The output is 0;
d. No Compilation error - The output is 10;
Q75
What is the result of compiling the following program
1. public class Test {
2. public static void main(String args[]) {
3. System.out.println("Before Try");
4. try {
5. } catch(java.io.IOException t) {
6. System.out.println("Inside Catch");
7. }
8. System.out.println("At the End");
9. }
10. }
Select one correct answer
a. No Compilation Error.
b. Compiler error complaining about the catch block where no IOException object can ever be thrown.
c. Compiler error - IOException not found. It must be imported in the first line of the code.
d. No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
Q76
Read the following snippet carefully
1. public synchronized void someMethod() {
2. //lots of code
3. try {
4. Thread.sleep(500);
5. } catch(InterruptedException e) {
6. //do some things here.
7. }
8. //more and more code here
9. }
Select all correct answers
a. The code causes compilation error - sleep cannot be called inside synchronized methods.
b. The Thread sleeps for at least 500 milliseconds in this method if not interrupted.
c. When the thread "goes to sleep" it releases the lock on the object.
d. The "sleeping" Threads always have the lock on the Object.
Q77
What will be the result of attempt to compile and the run the following code
1. public class ADirtyOne {
2. public static void main(String args[]) {
3. System.out.println(Math.abs(Integer.MIN_VALUE));
4. }
5. }
Select one correct answer
a. Causes a compilation error.
b. Causes no error and the value printed on the screen is less than zero.
c. Causes no error and the value printed on the screen is one more than Integer.MAX_VALUE
d. Will throw a runtime exception due to overflow - Integer.MAX_VALUE is less in magnitue than Integer.MIN_VALUE.
Q78
What is the result of attempting to compile and run the following program
1. public class Test {
2. public void method(Object o) {
3. System.out.println("Object Version");
4. }
5. public void method(String s) {
6. System.out.println("String Version");
7. }
8. public static void main(String args[]) {
9. Test test = new Test();
10. test.method(null);
11. }
12. }
Select one correct answer
a. The code does not compile.
b. The code compiles cleanly and shows "Object Version".
c. The code compiles cleanly and shows "String Version".
d. The code throws an Exception at Runtime.
Q79
What is the result of attempting to compile the following program
1. public class Test {
2. public void method(StringBuffer sb) {
3. System.out.println("StringBuffer Verion");
4. }
5. public void method(String s) {
6. System.out.println("String Version");
7. }
8. public static void main(String args[]) {
9. Test test = new Test();
10. test.method(null);
11. }
12. }
Select one correct answer
a. The code does not compile.
b. The code compiles correctly and shows "StringBuffer Version".
c. The code compiles correctly and shows "String Version".
d. The code throws an Exception at Runtime.
Q80
What is the requirement of the class which implements the following Interface
1. public interface Test {
2. void someMethod();
3. }
Select all correct answers
a. Should have someMethod which must be necessarily declared as public.
b. Should have someMethod which could be "friendly" or public
c. Should have someMethod which should not throw any checked exceptions.
d. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface
definition.
Q81
What is the result of attempting to compile and run the following program?
1. public class AStringQuestion {
2. static String s1;
3. static String s2;
4. public static void main(String args[]) {
5. s2 = s1+s2;
6. System.out.println(s2);
7. }
8. }
[Select one correct answer]
a. Will cause a compilation error.
b. Runtime Execption - NullPointerException in the 2nd line of the main method.
c. Will compile successfully and print "nullnull" on the screen.
d. Will compile successfully and print an empty line on the screen.
Q82
What is the result of attempting to compile and run the following program?
1. import java.io.*;
2. public class OutOut {
3. public static void main(String args[]) throws IOException {
4. PrintStream pr = new PrintStream(new FileOutputStream("outfile"));
5. System.out = pr;
6. System.out.println("Lets see what I see now??");
7. }
8. }
[Select one correct answer]
a. The code causes a compiler error. out is a declared final in System and cannot be assigned to pr.
b. The code causes a runtime Exception due the assignment to a final variable.
c. The code compiles and runs success fully. A file called "outfile" is created and "Lets see what I see now??"
is printed in the same.
d. The code cause a compiler error. PrintStream is an abstract class
Q83
What is the result of attempting to compiler and run the following piece of code.
1. import java.awt.*;
2. public class TestFrame extends Frame {
3. public TestFrame() {
4. Button one = new Button("One");
5. Button two = new Button("Two");
6. Button three = new Button("Three");
7. setLayout(new FlowLayout());
8. add(one);
9. add(two);
10. add(three);
11. setSize(1000,1000);
12. setVisible(true);
13. two.setVisible(false);
14. }
15. public static void main(String args[]) {
16. TestFrame tf = new TestFrame();
17. }
18. }
Select one correct answer
a. If the above code runs, the buttons - one and three are laid out in a single row from left to right with a
gap in between.
b. If the above code runs, the buttons - one and three are laid out in a single row from left to right with no
gap in between.
c. Code does not compile - a component can not be hidden after being added to a container.
d. Code gets compiled successfully but throws runtime Exception - a component can not be hidden after being added
to a container.
Q84
What is the result of attempting to compile and run the following piece of code?
1. import java.awt.*;
2. public class TestFrame extends Frame {
3. public TestFrame() {
4. CheckboxGroup chg = null;
5. Checkbox ch = new Checkbox("Test",true,chg);
6. setLayout(new FlowLayout());
7. add(ch);
8. pack();
9. setVisible(true);
10. }
11.
12. public static void main(String args[]) {
13. TestFrame tf = new TestFrame();
14. }
15. }
[Select one correct answer]
a. It will cause a compilation error as the checkbox group is null in the constructor.
b. It will compile successfully but throws a runtime exception because the checkbox group is null in the constructor
of the check box.
c. It will compile and run successfully. The checkbox appears as a single radio button which is always true
d. It will compile and run successfully. The checkbox bears its original appearence and does not appear as a
radio button.
Q85
What is the result of attempting to compile and run the following program?
1. public class Test {
2. private int i = j;
3. private int j = 10;
4.
5. public static void main(String args[]) {
6. System.out.println((new Test()).i);
7. }
8. }
[Select one correct answer]
a. Compiler error complaining about access restriction of private variables of Test.
b. Compiler error complaining about forward referencing.
c. No error - The output is 0;
d. No error - The output is 10;
Q86
What is the result of attempting to compile and run the following program
1. public class A {
2. private void method1() throws Exception {
3. throw new RuntimeException();
4. }
6. public void method2() {
7. try {
8. method1();
9. } catch(RuntimeException e) {
10. System.out.println("Caught Runtime Exception");
11. } catch(Exception e) {
12. System.out.println("Caught Exception");
13. }
14. }
15. public static void main(String args[]) {
16. A a = new A();
17. a.method2();
18. }
19. }
[Select one correct answer]
a. It will not compile.
b. It will compile and show - "Caught Exception".
c. It will compile and show - "Caught Runtime Exception".
d. It will compile and show both the messages one after another in the order they appear.
Q87
Assume that Cat is a class and String[] args is the argument passed to the public static void main(String args[])
method of the class. The class is executed with the following command line string
c;\somedirectory> java Cat
An expression in the main method is as follows
1. System.out.println(args.length);
What is the result of attempting to compile and run the snipped.
a. The above expression will cause a '0' appear on the command line.
b. It will throw a NullPointerException.
c. It will cause a nk line to appear.
d. It will cause a comilation error at line 1 due to length.
Q88
Given the following snippet of code
1. char c = -1;
What is the result of compiling the above snippet.
a. It will cause a compiler error as the range of character is between 0 and 2^16 - 1. Will request for an explicit
cast.
b. It will not cause a compiler error and c will have the value -1;
c. The variable c will not represent any ascii character.
d. The variable c will still be a unicode character.
Q89
Given the following code fragment.
1. switch( x ) {
2. case 100
3. System.out.println("One hundred"); break;
4. case 200
5. System.out.println("Two hundred"); break;
6. case 300
7. System.out.println("Three hundred"); break;
8. }
Choose all of the declarations of x that will not cause a compiler error.
a. byte x = 100;
b. short x = 200;
c. int x = 300;
d. long x = 400;
Q90
What happens to the file system when the following code is run and the myFile object is created? Assume that
name represents a valid path and file name for a file that does not presently exist.
1. File createFile(String name) {
2. File myFile = new File(name);
3. return myFile;
4. }
[Check all correct answers]
a. A new empty file with that name is created but not opened.
b. The current directory changes to that specified in name
c. A new emty file with that name is created and opened.
d. None of the above