Sunday, November 27, 2016

SCJP Java Interview Questions Part-II

Today, I am sharing Java SCJP/OCJP Written Test Interview Questions with answers,earlier i have shared SCJP/OCJP Interview Questions part-I. In this page we will discuss more SCJP interview Questions more. Those are preparing to appear SCJP/OCJP Written Test,they should brush up these questions and my earlier post as well. 

1) int[] myArray=new int[]{1,2,3,4,5}; What allows you to create a list from this array?

Ans:

A. List myList=myArray.asList();
B. List myList=Arrays.asList(myArray);
C. List myList=new ArrayList(myArray);
D. List myList=Collections.fromArray(myArray);

Answer: B

2)
 class A
{}
class B extends A{}
class C extends B{}
class D extends B{}
which three statements are true?(choose three)

A. The type List<A>is assignable to List
B. The type List<B>is assignable to List<A>
C. The type List<Object>is assignable to List<?>
D. The type List<D> is assignable to List<?extends B>
E. The type List<?extends A>is assignable to List<A>
F. The type List<Object>is assignable to any List reference
G. The type List<?extends B>is assignable to List<?extends A>

Answer: CDG

3) 

public static void search(List<String> list)
{
list.clear();
list.add("b");
list.add("a");
list.add("c");
System.out.println(Collections.binarySearch(list,"a"));
}

What is the result of calling search with a valid List implementation ?

A. 0
B. 1
C. 2
D. a
E. b
F. c
G. The result is undefined

Answer: G

4)

public class person
{
private name;
public Person(String name)
{
this.name=name;
}
public int hashCode()
{
return 420;
}
}
Which statement is true?

A. The time to find the value form HashMap with a Person Key depends on the size of the map.

B. Deleting a Person Key form a HashMap will delete all map entries for all keys of type person.

C. Inserting a second Person Object into a HashSet will cause the first Person object to be removed as a duplicate.

D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

Answer: A

5)

23. HashMap props=new HashMap();
24. props.put("key45","some value");
25. porps.put("key12","some other value");
25. props.put("key39","yet another value");
26. Set s=props.keySet();
27.//insert code here

What,inserted at line 27,will sort the keys in the props HashMap?

A. Arrays.sort(s);
B. s=new TreeSet(s);
C. Collections.sort(s);
D. s=new Sortedset(s);

Answer: B

6) 

11.Object[] myObject={new Integer(12),new String("abc"),new Integer(15),new 12.Boolean(true)};
13.Arrays.sort(myObject);
14.for(int i=0;i<myObject.length;i++)
15.System.out.println(myObject[i].toString());
16.System.out.println(" ");
}
What is the result?

A. Compailation fails due to an error in line 11
B. Compilation fails due to an error at line 13
C. A ClassCastException occurs in line 13
D. A ClassCastException occurs in line 15
E. The value of all four objects prints in natural order

Answer: C

7)

import java.util.*;
public class Old{
public static Object get0(List list){
return list.get(0);
}
}
Which three will compile successfully?(choose three)


A. Object o=Old.get0(new LinkedList());
B. Object o=Old.get0(new LinkedList<?>());
C. Object o=Old.get0(new LinkedList<String>());
D. Object o=Old.get0(new LinkedList<Object>());
E. String s=(String)Old.get0(new LinkedList<String>());

Answer: A,D,E

8)
Which two statements are true about the hashCode method?(Choose two)?

A. The hashCode method for a given class can be used to test for object equality and object inequality for that class

B. The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set.

C. The hashCode method for a given class can be used to test for object inequality,but Not object equality,for that class.

D. The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.

Answer: C,D

9)

A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add(0,object),but does NOT need to support quick random access.

What supports these requirements?

A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList

Answer: D

10)

public static void before(){
Set set=new TreeSet();
set.add("2");
set.add("3");
set.add("1");
Iterator itr=set.iterator();
while(itr.hasNext())
System.out.println(itr.next()+" ");
}

Which of the following statements are true?

A. the before() method will print 1 2
B. the before() method will print 1 2 3
C. the before() method will print three numbers,but the order cannot be determined
D. the before() method will not compile
E. the before() method will throw an exception at runtime.

Answer: E

Explanation: E is correct,you can not put both String and int into the same TreeSet. Without generics,the compiler has no way of knowing what type is appropriate for this TreeSet,so it allows everything to compile. At runtime, the TreeSet will try to sort the elements as they are added,and when it tries to compare an Integer with a String it will throw a ClassCastException. Note that although the before() method does not use generics,it does use autoboxing.



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...