Professional Documents
Culture Documents
All Questions 1-50
All Questions 1-50
#1 Given:
Answer : -------------------------
#2 Given:
The ClassA class can be compiled successfully by inserting one of three possible
options on line 3. When inserted separately, which three will allow compilation
to succeed? (Choose three.)
(1) int x = 6;
(2) short x = 3;
(3) char x = 'y';
(4) long x = 354;
(5) boolean x = true;
Answer : -------------------------
#3 Given:
Answer : -------------------------
#4 Given:
Which two additions will individually allow compilation to succeed? (Choose two.)
Answer : -------------------------
#5 Given:
(1) 2
(2) 4
(3) 6
(4) 7
Answer : -------------------------
#6 Given:
1. class IntType {
2. public String getType(String a, int b, char c) {
3. String holdit1 = new String();
4. holdit1 = a;
5. return holdit1;
6. }
7. }
8.
9. class OverType extends IntType {
10.
11. String holdit2 = new String();
12. holdit2 = holdit2.concat("This is ").concat(a);
13. return holdit2;
14. }
15.
16. public static void main(String args[]) {
17. OverType ot = new OverType();
18. String x = new String("x");
19. int y = 1;
20. char z = 'b';
21. System.out.println(ot.getType(x, y, z));
22. }
23. }
When inserted on line 10, which line will override the getType method, allowing compilation
to succeed and the output "This is x"?
Answer : -------------------------
#7 Given:
Answer : -------------------------
#8 Given:
1. import java.awt.*;
2.
3. public class Birthdays extends Frame {
4. Birthdays() {
5. super("Birthday Reminder");
6. String lblsP1[] = {"Name:", "Birthday:", "Address:"};
7. String butnsP2[] = {"Add", "Save", "Exit"};
8. Panel panelTop = new Panel();
9. Panel panelBot = new Panel();
10. panelTop.setLayout(new GridLayout(3,2,3,3));
11. for(int x = 0; x < lblsP1.length; x++) {
12. panelTop.add(new Label(lblsP1[x]));
13. panelTop.add(new TextField());
14. }
15. for(int y = 0; y < butnsP2.length; y++) {
16. panelBot.add(new Button(butnsP2[y]));
17. }
18. add(panelTop, BorderLayout.NORTH);
19. add(panelBot, BorderLayout.SOUTH);
20. }
21. }
Which main method should you add to the Birthdays class to allow the program to compile
and run with all defined fields properly displayed?
Answer : -------------------------
#9 Given:
Answer : -------------------------
Answer : -------------------------
Answer : -------------------------
Which of the following methods would be legal (individually) at line 2 in class Test2?
Answer : -------------------------
#13 You have been given a design document for a veterinary registration system for
implementation in Java technology. It states:
"A pet has a n o wn e r, a re g is t ra t io n dat e, and a v acc ina tio n-d ue dat e.
A c at is a p et th a t ha s a fl a g i nd i cati ng if it h as bee n n eut ere d, and a
tex tua l de s c ri p ti o n of it s m a rk i ng s ."
Given that the Pet class has already been defined, which of the following fields would be
appropriate for inclusion in the Cat class as members?
Answer : -------------------------
#14 You have been given a design document for a veterinary registration system for
implementation in Java. It states: "A pet has an owner, a registration date, and a
vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a
textual description of its markings." Given that the Pet class has already been defined and
you expect the Cat class to be used freely throughout the application, how would you make
the opening declaration of the Cat class, up to but not including the first opening brace?
Use only these words and spaces: boolean, Cat, class, Date, extends, Object, Owner, Pet,
private, protected, public, String.
Answer : -------------------------
(1) Value is 5
Value is 6
(2) This value is 5
This value is 6
(3) Value is 5
This value is 6
(4) This value is 5
Value is 6
(5) I was passed 5
I was passed 6
Answer : -------------------------
Which of the following are legitimate calls to construct instances of the Test class?
Answer : -------------------------
Which of the following forms of constructor must exist explicitly in the definition of the Base
class?
(1) Base() { }
(2) Base(int j) { }
(3) Base(int j, int k) { }
(4) Base(int j, int k, int l) { }
Answer : -------------------------
Answer : -------------------------
(1) a
(2) b
(3) c
(4) d
(5) e
Answer : -------------------------
(1) Given that Inner is a non-static class declared inside a public class Outer, and appropriate
constructor forms are defined, an instance of Inner may be constructed like this:
new Ou ter( ) . ne w I n ne r( )
(2) If an anonymous inner class inside the class Outer is defined to implement the interface
ActionListener, it may be constructed like this:
new Ou ter( ) . ne w A c ti on L is t en e r( )
(3) Given that Inner is a non-static class declared inside a public class Outer and appropriate
constructor forms are defined, an instance of Inner may be constructed in a static method
like this:
new In ner( )
(4) An anonymous class instance that implements the interface MyInterface may be
constructed and returned from a method like this: 1. return new MyInterface(int x) { 2. int
x; 3. public MyInterface(int x) { 4. this.x = x; 5. } 6. };
Answer : -------------------------
#21 Given:
class J {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private K k;
private int name;
public int name() {return name;}
public J(K k, int i) {this.k = k; name = i; notFinalized++;}
public void finalize() {
synchronized (k) {
System.out.print(name);
notFinalized--;
k.notify();
}
}
}
class K {
private void m1() {
J j = null;
for (int i = 0; i < 5; i++) {
j = new J(this, i); // 1
}
Runtime.getRuntime().gc(); // 2
synchronized (this) {
while (J.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
}
public static void main(String[] args) {
new K().m1();
}
}
When the processing of line 2 begins how many objects of type J that were created at line
1 are eligible for garbage collection?
(1) 0
(2) 1
(3) 4
(4) 5
(5) Can not be determined without more information
(6) Compiler error
(7) Run time error
(8) None of the above
Answer : -------------------------
#22 What is the output of the following code when compiled and run? Select two correct
answers
Answer : -------------------------
#23 What is the output of the following code when compiled and run? Select one correct answer
(1) Prints 3
(2) Error during compilation at line 4
(3) Error during compilation at line 5
(4) Prints 11
(5) Prints 0
Answer : -------------------------
(1) The garbage collection thread cannot outlive the last user thread
(2) The garbage collection can be forced by invoking System.gc().
(3) The garbage collection thread is a non-deamon thread
(4) The finalize() method is invoked at most once by the JVM for any given object
(5) The finalize() method may resurrect the object upon which it has been invoked
Answer : -------------------------
#25 What is the output of the following code when compiled and run? Select one correct
answer.
import java.io.*;
public class TechnoSample {
public static void main(String[] args) {
TechnoSampleSub myref = new TechnoSampleSub();
try{
myref.test();
}catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In TechnoSample");
throw new IOException();
}
}
class TechnoSampleSub extends TechnoSample {
void test() {
System.out.println("In TechnoSampleSub");
}
}
(1) Prints:
In T ec h no S am pl e Su b
(2) Prints:
In T ec h no S am pl e
(3) Prints:
In T ec h no S am pl e
In T ec h no S am pl e Su b
(4) Prints:
In T ec h no S am pl e Su b
In T ec h no S am pl e
Answer : -------------------------
#26 What is the output of the following code when compiled and run with the following
command line: java TechnoSample two three? Select two correct answers.
Answer : -------------------------
#27 Select two correct statements about the code given below?
class A{}
class B extends A implements E{} //line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
public static void main(String[] args) {
A a = new D(); //line 2
C c = new C(); //line 3
E e = (E)a; //line 4
B b = (B)e; //line 5
}
}
Answer : -------------------------
#28 How many objects are eligible for garbage collection immediately after line 1? Select one
correct answer.
(1) 0
(2) 1
(3) 2
(4) 3
(5) 4
Answer : -------------------------
#29 What is the output of the following code when compiled and run? Select one correct
answer.
Answer : -------------------------
#30 What is the output of the following code when compiled and run? Select one correct
answer.
(1) Prints 0 1 2 3 4
(2) Compilation error
(3) No output
(4) IllegalThreadStateException is thrown at runtime
(5) Prints 0 1 2 3 4 5
Answer : -------------------------
#31 What is the output of the following code when compiled and run? Select one correct
answer.
(1) Prints
He l l o W or l d
He l l o W or l d
(2) Prints
He l l o o rl d
He l l o o rl d
(3) Prints
He l l o o rl d
el l o o r ld
(4) Prints
el l o o r ld
el l o o r ld
Answer : -------------------------
#32 What is the output of the following code when compiled and run? Select two correct
answers. (Note: when an instance of a Vector is printed, its content appear between square
brackets [])
import java.util.*;
public class TechnoSample {
public static void main(String[] args) {
Vector col = new Vector();
col.add(new Integer(1));
col.add(new Integer("2"));
col.add(new Float(3.2d)); //line 1
col.add(col.elementAt(1));
col.setElementAt(col.elementAt(2),0);
System.out.println(col);
}
}
Answer : -------------------------
#33 What is the output of the following code when compiled and run? ('\u0048' is 'H' and
'\u000a' is a linefeed (LF)). Select two correct answers.
He l l o
Wo r l d!
Answer : -------------------------
#34 What is the output of the following code when compiled and run? Select two correct
answers.
Answer : -------------------------
#35 What is the output of the following code when compiled and run? Select two correct
answers.
(1) The output will always be the same whatever the underlying platform is
(2) Prints: End
(3) Prints:
0 1 2
En d
Answer : -------------------------
#36 What is the output of the following code when compiled and run? Select two correct
answers.
Answer : -------------------------
Answer : -------------------------
(1) The try-catch block that encloses myref.test(); is mandatory for the code to compile
(2) Prints: In TechnoSample
(3) Prints: In TechnoSampleSub
(4) Method test() in class TechnoSampleSub has no obligation to declare a throws clause
(5) An exception is thrown at runtime
Answer : -------------------------
import java.util.Date;
(4) the assignment 'd1 = d2' is rejected by the compiler because the Date class cannot
overload the operator '='
(5) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler because the
Date class cannot overload the operator '+'
Answer : -------------------------
S y st em . ou t .p r in t ln ( "Equ al" );
el s e
Answer : -------------------------
class Base{
static int oak=99;
}
(1) super.oak = 1;
(2) oak = 33;
(3) Base.oak = 22;
(4) oak = 50.1;
Answer : -------------------------
#42 What will happen when you attempt to compile and run the following code?
void fermin(int i) {
i++;
}
}
Answer : -------------------------
Which of the following lines if placed after the comment //Here will print out 0.
(1) System.out.println(i++);
(2) System.out.println(i+'0');
(3) System.out.println(i);
(4) System.out.println(i--);
Answer : -------------------------
#44 What will happen when you attempt to compile and run this code?
Answer : -------------------------
#45 What best describes the appearance of an application with the following code?
import java.awt.*;
FlowAp() {
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
}//End of constructor
}//End of Application
(1) A Frame with buttons marked One to Four placed on each edge
(2) A Frame with buutons marked One to four running from the top to bottom
(3) A Frame with one large button marked Four in the Centre
(4) An Error at run time indicating you have not set a LayoutManager
Answer : -------------------------
#46 What will happen when you attempt to compile and run the following code?
(1) A compile time error indicating that no run method is defined for the Thread class
(2) A run time error indicating that no run method is defined for the Thread class
(3) Clean compile and at run time the values 0 to 9 are printed out
(4) Clean compile but no output at runtime
Answer : -------------------------
#47
int Output = 10;
boolean b1 = false;
(1) Compilation error, attempting to perform binary comparison on logical data type
(2) Compilation and output of "We are equal 10"
(3) Compilation and output of "Not equal! 20"
(4) Compilation and output of "Not equal! 10"
Answer : -------------------------
(1) 15 0
20
(2) 15 0
15
(3) 20 0
20
(4) 0 15
20
Answer : -------------------------
#49 Given the code below, and making no other changes, which access modifiers (public,
protected or private) can legally be placed before myMethod() on line 3? If line 3 is left as
it is, which keywords can legally be placed before myMethod on line 8?
1. class HumptyDumpty
2. {
3. void myMethod() {}
4. }
5.
6. class HankyPanky extends HumptyDumpty
7. {
8. void myMethod() {}
9. }
(1) private or nothing (i.e. leaving it as it is) on line 3. Nothing (i.e. leaving it as it is) or
protected or public on line 8
(2) public or protected on line 3. private or nothing (i.e. leaving it as it is) on line 8
(3) nothing (i.e. leaving it as it is) or protected or public on line 3. private or nothing (i.e.
leaving it as it is) on line 8
(4) None of the above
Answer : -------------------------
#50 What is the result when you compile and run the following code?
Answer : -------------------------