|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|
|
|
|
#1
|
Given:
1. public abstract class Prod { 2. public abstract void prmth1(); 3. public static void prmth2() { 4. int mth2 = 30; 5. System.out.println("prmth2 = " + mth2); 6. } 7. public abstract void prmth3(); 8. }
What is the result?
|
|
(1)
|
Compilation succeeds
|
|
(2)
|
Compilation fails because of an
error on line 1
|
|
(3)
|
Compilation fails because of an
error on line 3
|
|
(4)
|
Compilation fails because of an
error on line 7
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#2
|
Given:
1. public class ClassA { 2. public static void main(String [] args) { 3. 4. switch(x) { 5. default: 6. System.out.println("Here it is."); 7. } 8. } 9. }
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:
1. public class NewGarb { 2. public static Object getIt() { 3. Object rg = new Integer(3); 4. Object dg[][] = new Object[1][2]; 5. dg[0][1] = rg; 6. dg[0][0] = rg; 7. rg = null; 8. return rg; 9. } 10. }
Which statement is true?
|
|
(1)
|
The NewGarb class will not
compile
|
|
(2)
|
The getIt() method must not be
declared as static
|
|
(3)
|
The NewGarb class compiles, but an
exception is received because dg is not set to null
|
|
(4)
|
The rg object is eligible for
garbage collection after a call to the getIt() method has returned
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#4
|
Given:
1. public class SetFor { 2. 3. public static void main(String [] args) { 4. 5. System.out.println("You will need to use " + c); 6. } 7. }
Which two additions will individually allow compilation to succeed?
(Choose two.)
|
|
(1)
|
"char c;" placed on line 2
|
|
(2)
|
"char c;" placed on line 4
|
|
(3)
|
"char c = 'f';" placed on line
2
|
|
(4)
|
"char c = 'f';" placed on line
4
|
|
(5)
|
"static char c;" placed on line
2
|
|
(6)
|
"char c = new char();" placed on
line 4
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#5
|
Given:
1. public class TeSet { 2. public static void main(String args[]) { 3. int m = 2; 4. int p = 1; 5. int t = 0; 6. for(;p < 5;p++) { 7. if(t++ > m) { 8. m = p + t; 9. } 10. } 11. System.out.println("t equals " + t); 12. } 13. }
What is the resulting value of t?
|
|
(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"?
|
|
(1)
|
public Char getType(String a, int b,
char c) {
|
|
(2)
|
public Char getType(int b, String a,
char c) {
|
|
(3)
|
public String getType(String a, int
b, char c) {
|
|
(4)
|
public String getType(int b, String
a, char c) {
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#7
|
Given:
1. public class MyThread implements Runnable { 2. private String holdA = "This is "; 3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10}; 4. 5. public static void main(String args[]) { 6. MyThread z = new MyThread(); 7. (new Thread(z)).start(); 8. (new Thread(z)).start(); 9. } 10. 11. public synchronized void run() { 12. for(int w = 0;w < 10;w++) { 13. System.out.println(holdA + holdB[w] + "."); 14. } 15. } 16. }
What is the result?
|
|
(1)
|
Compilation fails because of an
error on line 6
|
|
(2)
|
Compilation fails because of an
error on line 11
|
|
(3)
|
Compilation fails because of errors
on lines 7 and 8
|
|
(4)
|
Compilation succeeds and the program
prints each value in the holdB array at the end of the "This is " line.
Each value is printed two times before the program ends, and the values
are not printed in sequential order
|
|
(5)
|
Compilation succeeds & the prog.
prints each val in the holdB array at the end of the "This is " line. Each
val is printed in order from 1-10 & after the val 10 prints, it starts
printing the vals 1-10 in order again
|
|
|
|
|
|
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?
|
|
(1)
|
public static void main(String
args[]) { Frame.visible = true; }
|
|
(2)
|
public static void main(String
args[]) { Frame f = new Frame(); f.setVisible(true);}
|
|
(3)
|
public static void main(String
args[]) { Birthdays b = new Birthdays(); b.pack(); b.setVisible(true);
}
|
|
(4)
|
public static void main(String
args[]) { Frame f = Birthdays.new Frame(); f.pack(); f.visible = true;
}
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#9
|
Given:
1. public class GetIt { 2. public static void main(String args[]) { 3. double x[] = {10.2, 9.1, 8.7}; 4. int i[] = new int[3]; 5. for(int a = 0;a < (x.length);a++) { 6. 7. System.out.println(i[a]); 8. } 9. } 10. }
The GetIt class should print the following: 11 10 9
Which line should you insert on line 6 to accomplish this?
|
|
(1)
|
i[a] = ((int)Math.min(x[a]));
|
|
(2)
|
i[a] = ((int)Math.max(x[a]));
|
|
(3)
|
i[a] =
((int)Math.ceil(x[a]));
|
|
(4)
|
i[a] =
((int)Math.floor(x[a]));
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#10
|
Which statement about the Map
interface is true?
|
|
(1)
|
Entries are placed in a Map using
the values() method
|
|
(2)
|
Entries are placed in a Map using
the entrySet() method
|
|
(3)
|
A key/value association is added to
a Map using the put() method
|
|
(4)
|
A key/value association is added to
a Map using the putAll() method
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#11
|
Consider this class:
1. public class Test1 { 2. public float aMethod(float a, float b) { 3. } 4. 5. }
Which of the following methods would be legal if added (individually) at
line 4?
|
|
(1)
|
public int aMethod(int a, int b) {
}
|
|
(2)
|
public float aMethod(float a, float
b) { }
|
|
(3)
|
public float aMethod(float a, float
b, int c) throws _Exception { }
|
|
(4)
|
public float aMethod(float c, float
d) { }
|
|
(5)
|
private float aMethod(int a, int b,
int c) { }
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#12
|
Consider these classes, defined in
separate source files:
1. public class Test1 { 2. public float aMethod(float a, float b) throws IOException { 3. } 4. }
1. public class Test2 extends Test1 { 2. 3. }
Which of the following methods would be legal (individually) at line 2
in class Test2?
|
|
(1)
|
float aMethod(float a, float b) {
}
|
|
(2)
|
public int aMethod(int a, int b)
throws Exception { }
|
|
(3)
|
public float aMethod(float a, float
b) throws _Exception { }
|
|
(4)
|
public float aMethod(float p, float
q) { }
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#13
|
You have been given a design
document for a veterinary registration system for implementation in Java
technology. 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, which of the
following fields would be appropriate for inclusion in the Cat class as
members?
|
|
(1)
|
Pet thePet;
|
|
(2)
|
Date registered;
|
|
(3)
|
Date vaccinationDue;
|
|
(4)
|
Cat theCat;
|
|
(5)
|
boolean neutered;
|
|
(6)
|
String markings;
|
|
|
|
|
|
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 :
-------------------------
|
|
|
|
|
#15
|
Consider the following classes,
declared in separate source files:
1. public class Base { 2. public void method(int i) { 3. System.out.println("Value is " + i); 4. } 5. }
1. public class Sub extends Base { 2. public void method(int j) { 3. System.out.println("This value is " + j); 4. } 5. public void method(String s) { 6. System.out.println("I was passed " + s); 7. } 8. public static void main(String args[]) { 9. Base b1 = new Base(); 10. Base b2 = new Sub(); 11. b1.method(5); 12. b2.method(6); 13. } 14. }
What output results when the main method of the class Sub is run?
|
|
(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 :
-------------------------
|
|
|
|
|
#16
|
Consider the following class
definition:
1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. }
Which of the following are legitimate calls to construct instances of
the Test class?
|
|
(1)
|
Test t = new Test();
|
|
(2)
|
Test t = new Test(1);
|
|
(3)
|
Test t = new Test(1, 2);
|
|
(4)
|
Test t = new Test(1, 2, 3);
|
|
(5)
|
Test t = (new Base()).new
Test(1);
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#17
|
Consider the following class
definition:
1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. }
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 :
-------------------------
|
|
|
|
|
#18
|
Which of the following statements
are true?
|
|
(1)
|
An inner class may be declared
private
|
|
(2)
|
An inner class may be declared
static
|
|
(3)
|
An inner class defined in a method
should always be anonymous
|
|
(4)
|
An inner class defined in a method
can access all the method local variables
|
|
(5)
|
Construction of an inner class may
require an instance of the outer class
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#19
|
Consider the following definition:
1. public class Outer { 2. public int a = 1; 3. private int b = 2; 4. public void method(final int c) { 5. int d = 3; 6. class Inner { 7. private void iMethod(int e) { 8. 9. } 10. } 11. } 12. }
Which variables may be referenced correctly at line 8?
|
|
(1)
|
a
|
|
(2)
|
b
|
|
(3)
|
c
|
|
(4)
|
d
|
|
(5)
|
e
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#20
|
Which of the following statements
are true?
|
|
(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 Outer().new Inner()
|
|
(2)
|
If an anonymous inner class inside
the class Outer is defined to implement the interface ActionListener, it
may be constructed like this:
new Outer().new ActionListener()
|
|
(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 Inner()
|
|
(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
1 public class Sample { 2 public static void main(String[] args){ 3 int y=0; 4 int x=z=1; 5 System.out.println(y+","+x+","+z); 6 } 7 }
|
|
(1)
|
Prints 0,1,1
|
|
(2)
|
Error during compilation at line
3
|
|
(3)
|
Prints 0,0,1
|
|
(4)
|
Error during compilation at line
4
|
|
(5)
|
Error during compilation at line
5
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#23
|
What is the output of the following
code when compiled and run? Select one correct answer
1 public class Sample { 2 public static void main(String[] args){ 3 int j = 017; 4 int i = (byte)j >> 2; 5 System.out.println(Integer.toBinaryString(i)); 6 } 7 }
|
|
(1)
|
Prints 3
|
|
(2)
|
Error during compilation at line
4
|
|
(3)
|
Error during compilation at line
5
|
|
(4)
|
Prints 11
|
|
(5)
|
Prints 0
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#24
|
Select three correct statements:
|
|
(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 TechnoSampleSub
|
|
(2)
|
Prints:
In TechnoSample
|
|
(3)
|
Prints:
In TechnoSample
In TechnoSampleSub
|
|
(4)
|
Prints:
In TechnoSampleSub
In TechnoSample
|
|
(5)
|
The code does not compile
|
|
|
|
|
|
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.
public class TechnoSample { public static void main(String[] args) throws Exception { int i=2; boolean b = true; throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2)); } }
|
|
(1)
|
The exception message is Values
are:false,3,true
|
|
(2)
|
The exception message is Values
are:true,2,false
|
|
(3)
|
The exception message is Values
are:false,2,true
|
|
(4)
|
The final value of b is false
|
|
(5)
|
An exception is thrown at
runtime
|
|
|
|
|
|
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 } }
|
|
(1)
|
The code compiles without error and
runs fine
|
|
(2)
|
Compilation error on line 1 because
interface E is not yet declared (forward-referencing)
|
|
(3)
|
Compilation error on line 4 because
class A does not implement interface E
|
|
(4)
|
The cast on line 4 is
mandatory
|
|
(5)
|
The cast on line 5 is not
mandatory
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#28
|
How many objects are eligible for
garbage collection immediately after line 1? Select one correct answer.
public class TechnoGC { public static void main(String[] args) { TechnoGC tGC = new TechnoGC(); tGC.doSomething(); //line 1 Thread.sleep(20000); }
public void doSomething(){ Object[] objArray = new Object[2]; for(int i = 0 ; i < objArray.length ; i++) { objArray[i] = new Object(); } } }
|
|
(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.
public class TechnoSample { public static void main(String[] args) { try { int i = (int)(Math.random()*10); if(i<=5) System.out.println("i = "+i); else throw new Exception("i > 5"); } catch (Exception e){ System.err.println(e.getMessage()+" (i="+i+")"); } } }
|
|
(1)
|
The output cannot be
determined
|
|
(2)
|
Compilation error
|
|
(3)
|
An exception is thrown at
runtime
|
|
(4)
|
Output is i = 2
|
|
(5)
|
Output is i > 5 (i=6)
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#30
|
What is the output of the following
code when compiled and run? Select one correct answer.
public class TechnoSample { public static void main(String[] args) { new TechnoSample().doSomething(); }
public void doSomething(){ int i=5; Thread t = new Thread(new Runnable(){ public void run(){ for(int j=0;j<=i;j++){ System.out.print(" "+j); } } }); t.start(); } }
|
|
(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.
public class TechnoSample { public static void main(String[] args) { StringBuffer buf1 = new StringBuffer("Hello W"); addSomeStuff(buf1); System.out.println(buf1.toString()); }
public static void addSomeStuff(StringBuffer buf){ StringBuffer b = buf.replace(6,10,"orld"); System.out.println(b.delete(0,1).toString()); } }
|
|
(1)
|
Prints
Hello World
Hello World
|
|
(2)
|
Prints
Hello orld
Hello orld
|
|
(3)
|
Prints
Hello orld
ello orld
|
|
(4)
|
Prints
ello orld
ello orld
|
|
(5)
|
Compilation error
|
|
|
|
|
|
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); } }
|
|
(1)
|
Compilation error on line 1
|
|
(2)
|
Only line 1 won't compile
|
|
(3)
|
The code compiles and runs
fine
|
|
(4)
|
Prints [3.2, 2, 3.2, 2]
|
|
(5)
|
Prints [1, 2, 3.2, 2]
|
|
|
|
|
|
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.
public class TechnoSample { public static void main(String[] args) { String s = "\u0048ello\u000aWorld!"; System.out.println(s); } }
|
|
(1)
|
Prints:
Hello
World!
|
|
(2)
|
Prints Hello World!(linefeed is
ignored)
|
|
(3)
|
Compilation error
|
|
(4)
|
Compiles fine, but an exception is
thrown at runtime
|
|
(5)
|
Unicode characters like '\u0048' may
be used to create String literals
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#34
|
What is the output of the following
code when compiled and run? Select two correct answers.
public class TechnoSample { public static void main(String[] args) { int i = 2; try { if((i/=(int)Math.floor(Math.random())) > 1) System.out.println("No arithmetic exception"); } catch (ArithmeticException ae){ System.err.println("Arithmetic exception caught"); } } }
|
|
(1)
|
Compilation error
|
|
(2)
|
No arithmetic exception will never
be printed
|
|
(3)
|
The final value of i is 2
|
|
(4)
|
The final value of i is 0
|
|
(5)
|
The try-catch block is necessary for
the code to compile
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#35
|
What is the output of the following
code when compiled and run? Select two correct answers.
public class TechnoSample { public static void main(String[] args) throws Exception{ Thread t1 = new Thread(getRunnable(3)); Thread t2 = new Thread(getRunnable(4)); t1.join(); System.out.println("End"); }
public static Runnable getRunnable(final int id){ return new Runnable(){ public void run(){ for(int i = 0; i < id; i++){ System.out.print(" "+i); } } }; } }
|
|
(1)
|
The output will always be the same
whatever the underlying platform is
|
|
(2)
|
Prints: End
|
|
(3)
|
Prints:
0 1 2
End
|
|
(4)
|
The output cannot be
determined
|
|
(5)
|
Compilation error
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#36
|
What is the output of the following
code when compiled and run? Select two correct answers.
public class TechnoSample { public static void main(String[] args){ for(int i = 0; i < 10; i++){ System.out.println(getPrimitive(127)); //line 1 } } public static int getPrimitive(byte b){ //line 2 return (short)(Math.random()*b); //line 3 } }
|
|
(1)
|
Compilation error on line 1
|
|
(2)
|
Compilation error on line 2
|
|
(3)
|
Compilation error on line 3
|
|
(4)
|
Line 3 compiles fine
|
|
(5)
|
Prints 10 random numbers between 0
and 127
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#37
|
Select three correct statements.
|
|
(1)
|
A static method may override another
static method
|
|
(2)
|
A static method cannot override a
non-static method
|
|
(3)
|
A non-static method cannot override
a static method
|
|
(4)
|
A non-static method may be
overloaded by a static method
|
|
(5)
|
A synchronized method cannot be
overridden
|
|
|
|
|
|
Answer :
-------------------------
|
|
|
|
|
#38
|
Select three correct statements
about the following code.
public class TechnoSample { public static void main(String[] args) { TechnoSample myref = new TechnoSampleSub(); try{ myref.test(); }catch(Exception e){} } void test() throws Exception{ System.out.println("In TechnoSample"); throw new Exception(); } } class TechnoSample Sub extends TechnoSample { void test() { System.out.println("In TechnoSampleSub"); } }
|
|
(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 :
-------------------------
|
|
|
|
|
#39
|
Given the following code:
import java.util.Date; public class Example { public static void main(String args[]) { Date d1 = new Date (99, 11, 31); Date d2 = new Date (99, 11, 31); method(d1, d2); System.out.println("d1 is " + d1 + "\nd2 is " + d2); } public static void method(Date d1, Date d2) { d2.setYear (100); d1 = d2; } }
Which one or more of the following correctly describe the behavior when
this program is compiled and run?
|
|
(1)
|
compilation is successful and the
output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Fri December 31 00:00:00 GMT 1999
|
|
(2)
|
compilation is successful and the
output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Sun December 31 00:00:00 GMT 2000
|
|
(3)
|
compilation is successful and the
output is:
d1 is Sun December 31 00:00:00 GMT 2000 d2 is Sun December 31 00:00:00 GMT 2000
|
|
(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 '+'
|
|
|
|
|
|
|