All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Toughest Questions for Oracle Java Programmer (SCJP 6.0) Certification
PostPosted: Sat Sep 25, 2010 11:17 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 3577

Toughest Questions for - Oracle Java Programmer (SCJP 6.0) Certification



Q. No. 1

Which lines of code will not be acceptable to the compiler?




import java.*; //1
public abstract class InternalLogic //2
{
float density = 20.0; //3
public class Doer //4
{
void do() //5
{
//lot of valid code.
}
}
}


(a) 1
(b) 2
(c) 3
(d) 4
(e) 5






Q. No. 2

What is the result of executing the following fragment of code:



boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2)
{
System.out.println("true");
}
else
{
System.out.println("false");
}


(a) Compile time error.
(b) It will print true.
(c) It will print false.
(d) Runtime error.
(e) It will print nothing.






Q. No. 3

What happens when you try to compile and run the following class...


public class TestClass
{
public static void main(String[] args) throws Exception
{
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);
}
}


(a) It throws an OverFlowException.
(b) It will print two same -ive numbers.
(c) It will print two different -ive numbers.
(d) It will print one -ive and one +ive number of same magnitude.
(e) It will print one -ive and one +ive number of different magnitude.






Q. No. 4

Consider the following code:


Float f1 = new Float(Float.NaN);
Float f2 = new Float(Float.NaN);
System.out.println( ""+ (f1 == f2)+" "+f1.equals(f2)+ " "+(Float.NaN == Float.NaN) );

What will it print ?


(a) It will print false true false
(b) It will print false true true
(c) It will print false false false
(d) It will print true true false
(e) It will print true true true






Q. No. 5

Consider the following code :


String id = c.readLine("%s", "Enter UserId:"); //1
System.out.println("userid is " + id); //2
String pwd = c.readPassword("%s", "Enter Password :"); //3
System.out.println("password is " + pwd); //4

Assuming that c is a valid reference to java.io.Console and that a user types jack as userid and jj123
as password, what will be the output on the console?


(a) Enter UserId:jack
userid is jack
Enter Password :
password is jj123
(b) Enter UserId:jack
userid is jack
Enter Password :*****
password is jj123
(c) Enter UserId:jack
userid is jack
Enter Password :
password is ****
(d) Enter UserId:jack
userid is jack
Enter Password : password is jj123
(e) It will not compile.






Q. No. 6

Given the following classes and declarations, which of these statements about //1 and //2 are true?



class A
{
private int i = 10;
public void f(){}
public void g(){}
}

class B extends A
{
public int i = 20;
public void g(){}
}

public class C
{
A a = new A();//1
A b = new B();//2
}


(a) System.out.println(b.i); will print 10.
(b) The statement b.f( ); will give compile time error..
(c) System.out.println(b.i); will print 20
(d) All the above are correct.
(e) None of the above statements are correct.






Q. No. 7

Which statements, when inserted at line 1, will cause a runtime exception ?



class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest
{
public static void main(String args[])
{
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
// insert statement here
}
}


(a) b = b1;
(b) b2 = b;
(c) b1 = (B1) b;
(d) b2 = (B2) b1;
(e) b1 = (B) b1;






Q. No. 8

Which of the following statements are true?



(a) A program ends when all daemon threads end.
(b) A program ends when all non-daemon threads end.
(c) A program ends when all threads end.
(d) A daemon thread is a low priority thread that never stops but keeps running in the back ground.
(e) A user thread cannot stop a non-user or daemon thread.






Q. No. 9

Write down the command line switch that allows you to enable assertions for the system level classes

[i.e. the classes that come bundled with the JDK].


(a) esa
(b) enablesystemassertions
(c) -esa
(d) -enablesystemassertions






Q. No. 10

Consider the following code:

(See Exhibit)
Which of the following options correctly instantiates a TestInner object?



public class TestOuter
{
public void myOuterMethod()
{
// 1
}
public class TestInner { }
public static void main(String[] args)
{
TestOuter to = new TestOuter();
// 2
}
}


(a) new TestInner(); at //1
(b) new TestInner(); at //2
(c) new to.TestInner(); at //2
(d) new TestOuter.TestInner(); at //2
(e) new TestOuter().new TestInner(); at //1






Q. No. 11

Consider the following program:


import java.util.*;
class Book {
private String title, isbn;
public boolean equals(Object o){
return (o instanceof Book && ((Book)o).isbn.equals(this.isbn));
}

// ... setters and getters for title and isbn
}

class BookStore {
Map<Book, Integer> map = new HashMap<Book, Integer>();
int getNumberOfCopies(Book b){
return map.get(b);
}
public void addBook(Book b, int numberofcopies){
map.put(b, numberofcopies);
}
// ... other useful methods.
}

public class TestClass {
static BookStore bs = new BookStore();

public static void main(String[] args) {
Book b = new Book(); b.setIsbn("111");
bs.addBook(b, 10);
System.out.println(bs.getNumberOfCopies(b));

b = new Book(); b.setIsbn("111");
System.out.println(bs.getNumberOfCopies(b));
}
}

What will it print when it is compiled and run?


(a) It will compile with a warning and run without throwing an exception.
(b) It will compile without a warning and will throw an exception when run.
(c) It will compile and run without warning and exception.
(d) It will print 10 and -1.
(e) It will print 10 and 0.
(f) It will print 10.






Q. No. 12

Given :


class MyStringComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
int s1 = ((String) o1).length();
int s2 = ((String) o2).length();
return s1 - s2;
}
}

and

static String[] sa = { "d", "bbb", "aaaa" };

Select correct statements.


(a) This is not a valid Comparator implementation.
(b) Arrays.binarySearch(sa, "cc", new MyStringComparator()); will return -2.
(c) Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0.
(d) Arrays.binarySearch(sa, "c", new MyStringComparator()); will return -1.
(e) Arrays.binarySearch(sa, "c", new MyStringComparator()); will throw an exception.






Q. No. 13

What will be the result of compiling and running the following code?



class Base
{
public short getValue(){ return 1; } //1
}
class Base2 extends Base
{
public byte getValue(){ return 2; } //2
}
public class TestClass
{
public static void main(String[] args)
{
Base b = new Base2();
System.out.println(b.getValue()); //3
}
}


(a) It will print 1
(b) It will print 2.
(c) Compile time error at //1
(d) Compile time error at //2
(e) Compile time error at //3






Q. No. 14

Consider the following code:



class A
{
}
public class TestClass
{
public class A
{
public void m() { }
}
class B extends A
{
}
public static void main(String args[])
{
new TestClass().new A() { public void m() { } };
}
}


(a) This program will not compile.
(b) class created inside the main method is static.
(c) class created inside the main method is final.
(d) Objects of class B cannot be created inside the main method just by doing "new B()"
(e) None of these.






Q. No. 15

Which of the given statements will compile and run without exceptions?



(a) System.out.printf("\"%-5c\"", 'c');
(b) System.out.printf("\"%+0d\"" , 100);
(c) System.out.printf("\"$%(,9.2f\"", -1222.2);
(d) System.out.printf("%b", 100);
(e) System.out.printf("\"%c\"", new Character('d'));
(f) System.out.printf("%s", new Object());
(g) System.out.printf("\"%f\"", -100);






Q. No. 16

What will the following code snippet return?


"name:john;;sex:m;;".split(";");


(a) A String array containing 3 elements.
(b) A List containing 2 elements.
(c) A List containing 4 elements.
(d) A String array containing 4 elements.
(e) A String array containing 2 elements.






Q. No. 17

Consider the following code.


import java.text.*;
import java.util.*;
public class TestClass
{
public static void main(String[] args) throws Exception
{
Date d = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
df.setLocale(new Locale("fr", "FR"));

String s = // 1 insert code here.

System.out.println(s);
}
}

What should be inserted at //1 above so that it will print the date in French format?


(a) df.format(d);
(b) df.formatDate(d);
(c) df.toString(d);
(d) df.getString(d);
(e) None of these.






Q. No. 18

Consider the following class:


class MessageBuffer
{
private static MessageBuffer mb = new MessageBuffer();

private MessageBuffer(){ }

public synchronized static MessageBuffer getInstance(){ return mb; }

private StringBuilder sb = new StringBuilder();

public void addMessage(String msg)
{
sb.append(msg+"|");
}

public void dumpMessage()
{
System.out.println(sb.toString());
}
}

This class is used by multiple threads to add messages. Which of the given options correctly describe
its behavior?


(a) Messages added by multiple threads may not be stored correctly.
(b) It can be made thread safe if StringBuilder is replaced by StringBuffer.
(c) It can be made thread safe if only the addMessage() method is synchronized.
(d) It cannot be made thread safe if method dumpMethod() is removed.
(e) None of these is correct.






Q. No. 19

Which of these methods are defined in the Map interface?



(a) contains(Object o)
(b) addAll(Collection c)
(c) remove(Object o)
(d) values( )
(e) toArray( )






Q. No. 20

Which variables of the encapsulating class, can an inner class access if the inner class is defined in

a static method of encapsulating class?


(a) All static variables
(b) All final instance variables
(c) All instance variables
(d) All automatic variables.
(e) All final automatic variables






Q. No. 21

Given the following program, which statements are true?



// Filename: TestClass.java
public class TestClass
{
public static void main(String args[])
{
A[] a, a1;
B[] b;
a = new A[10]; a1 = a;
b = new B[20];
a = b; // 1
b = (B[]) a; // 2
b = (B[]) a1; // 3
}
}
class A { }
class B extends A { }


(a) Compile time error at line 3.
(b) The program will throw a java.lang.ClassCastException at the line labeled 2 when run.
(c) The program will throw a java.lang.ClassCastException at the line labeled 3 when run.
(d) The program will compile and run if the (B[ ] ) cast in the line 2 and the whole line 3 is removed.
(e) The cast at line 2 is needed.






Q. No. 22

Which statements regarding the following code are correct?



class A extends Thread
{
public void run()
{
System.out.println("Starting loop");
while(true){}; //1
System.out.println("Ending loop");
}
}
public class TestClass
{
public static void main(String args[]) throws Exception
{
A a = new A();
a.start();
Thread.sleep(1000);
a.interrupt();
}
}


(a) This will not compile.
(b) It will run and end cleanly if //1 is replaced with while(!isInterrupted()) { };
(c) It will run and will end with an InterruptedException stack trace on the command line.
(d) It will run and will end with an InterruptedException if //1 is replaced with while(!isInterrupted()) { };
(e) None of these.






Q. No. 23

Consider the following program:

(See Exhibit.)
Which of the following is correct regarding the execution of the given program?



public class TestClass extends Thread
{
private static int threadcounter = 0;
public void run()
{
threadcounter++;
System.out.println(threadcounter);
}
public static void main(String[] args) throws Exception
{
for(int i=0; i<10; i++)
{
synchronized(TestClass.class)
{
new TestClass().start();
}
}
}
}


(a) Numbers 1 to 10 will be printed (none repeated or missed) in a serial order.
(b) Numbers 1 to 10 will be printed (none repeated or missed) in any order.
(c) Total of 10 numbers will be printed
(d) The final value of threadcounter just before the program terminates will be 10.
(e) The final value of threadcounter just before the program terminates may be less than 10.






Q. No. 24

How many objects will be eligible for GC just after the method returns?



public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}


(a) 0
(b) 1
(c) 2
(d) 3
(e) 4






Q. No. 25

Which statements regarding the following code are correct?


class Outer
{
private void Outer() { }
protected class Inner
{
}
}


(a) This code will not compile.
(b) Constructor for Outer is public.
(c) Constructor for Outer is private.
(d) Constructor for Inner is public.
(e) Constructor for Inner is protected.






Q. No. 26

Consider the following code:


public class AssertErrorTest
{
public void robustMethod(int[] intArray) throws AssertionError
{
int[] newIA = //get new array by processing intArray
assert newIA != intArray;
}
}

Which of the following declarations of robustMethod(int[] intArray) are valid in a subclass of the above
class?


(a) public void robustMethod(int[] intArray)
(b) public void robustMethod(int[] intArray) throws Exception
(c) public void robustMethod(int[] intArray) throws Throwable
(d) public void robustMethod(int[] intArray) throws Error
(e) public void robustMethod(int[] intArray) throws RuntimeException







Top
 Profile Send private message  
 
 Post subject: Re: Toughest Questions for Oracle Java Programmer (SCJP 6.0) Certification
PostPosted: Wed Apr 02, 2014 8:45 pm 
Offline

Joined: Thu Mar 27, 2014 11:35 am
Posts: 19
Correct answer is B. But two objects are local and return type is void. Is 'a' subject to gc and is 'str' not? Or vice-versa? Why? Thanks.


Code:
Q. No. 24

How many objects will be eligible for GC just after the method returns?





public void compute(Object p)

{

 Object a = new Object();

 int x = 100;

 String str = "abc";

}



(a) 0
(b) 1
(c) 2
(d) 3
(e) 4


Top
 Profile Send private message  
 
 Post subject: Re: Toughest Questions for Oracle Java Programmer (SCJP 6.0) Certification
PostPosted: Mon Apr 07, 2014 10:01 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 3577
__Bill wrote:
Correct answer is B. But two objects are local and return type is void. Is 'a' subject to gc and is 'str' not? Or vice-versa? Why? Thanks.


a is, str is not. Please check out the basics on garbage collection and strings.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum