Showing posts with label scjp written Test. Show all posts
Showing posts with label scjp written Test. Show all posts

Friday, February 24, 2017

SCJP Written Test Questions

In this post i am sharing SCJP/OCJP(Oracle Certified Java Programming) Written Test Questions and Answers. These questions are very helpful who want become OJCP Certified candidate. Then he/she must give Written Test. Now we will see some of the important Questions and answers

1) What is the appropriate definition of the hashCode method in class Person?
public class Person{
private String name,comment;

private int age;
public Person(String n,int a,String c)
{
name=n;
age=a;
comment=c;
}
public boolean equals(Object o){
if(!o (instanceof Person))return false;
Person p=(Person)o;
return age==p.age&&name.equals(p.name);
}
}

A. return super.hashCode();
B. return name.hashCode()+age * 7;
C. return name.hashCode()+comment.hashCode()/2;
D.return name.hashCode()+comment.hashCode()/2-age * 3;

Answer:  B

2) Given this method in a class:

public String toString()
{
StringBuffer sb=new StringBuffer();
sb.append('<');
sb.append(this.name);
sb.append('>');
return sb.toString();
}

Which Statement is True?

A. This Code is NOT thread-safe
B. The programmer can replace StringBuffer with StringBuilder with no other changes.
C. This code will perform poorly. For better performance,the code should be rewritten like follows: return "<" + this.name + ">";
D. This code will perform well and converting the code to use StringBuilder will not enhance the performance.

Answer : B

3) Given
1.public void testIfA()
2.{
3.if(testIfB("TRUE"))
4.{
5.System.out.println("True");

6.}
7.else
8.{
9.System.out.prinltn("Not True");

10.}
11.public Boolean testIfB(String str){
12.return Boolean.valueOf(str);
13.}

What is the result when method testIfA is invoked?

A. True
B. Not True
C. An exception is thrown at runtime
D. Compilation fails because of an error at line number 3
E: Compilation fails because of an error at line number 12

Answer: A 


4. Given
public static void test(String str)
{
int check=4;

if(check=str.length())
{
System.out.println(str.charAt(check=1)+",");
}
else
{
System.out.println(str.charAt(0)+",");
}
}
and the invocation:
test("four");
test("tee");
test("to");
What is the result?

A. r,t,t
B. r,e,o
C. Compilation fails
D. An Exception is thrown at runtime

Answer: C

5)
public class person
{
private String name;
public Person(String name)
{
this.name=name;

}
public boolean equals(Object o)
{
if(!o instanceof Person)
return false;
Person p=new (Person)o;
return p.name.equals(this.name);
}
}
Which statement is true?

A. compilation fails because the hashCode method is not overridden
B. A HashSet could contain multiple Person Objects with the same name
C. All Person objects will have the same hash code because the hashCode method is not overridden
D. If a HashSet contains more than one person Object with name ="Fred",then removing another Person,also with name ="Fred" will remove them all

Answer: B

6) Given

class Polish
{
public static void main(String[] args)
{
int x=4;
StringBuffer sb=new StringBuffer("..fedcba");
sb.delete(3,6);
sb.insert(3,"az");
if(sb.length()>6)x=sb.indexOf("b"));
sb.delete((x-3),(x-2));
System.out.println(sb);
}
}
What is the result?

A. .faza
B. .fzba
C. ..azba
D. .fazba
E. ..fezba
F. Compailation fails
G. Exception thrown at run time

Answer:

C is correct because StringBuffer methods use zero-based indexes,and that ending indexes are typically exclusive

Recommended to Read:

SCJP Java written Test Part 1
SCJP Java written Test Part 2
IT Basics for Tech Developers 
When to use abstract class and when interface in Java




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.



Tuesday, November 22, 2016

SCJP Java Interview Questions part-I

In this blog, I am sharing SCJP written Test Exam Questions and answers. Those are preparing for SCJP/OCJP Examination must read these interview questions. We cover here inheritance,polymorphism,Exception Handling and Multi-threading concepts. I highly recommended  take the Core Java Course,before attempting SCJP/OCJP TEST.
1) Given 
int x=0;
int y=10;
do{
y--;
++x;
}
while(x<5);
System.out.println(x+","+y);

then what is the result?

A. 5,6
B. 5,5
C. 6,5
D. 6,6

Answer: B

2)
Choose the correct statement:

a. "A extends B" is correct if and only if A is a class and B is an interface
b. "A extends B" is correct if and only if A is an interface and B is a class
c. " A extends B" is correct if A and B are either both classes or both interfaces
d. "A extends B" is correct for all combinations of A and B being classes or interfaces

Answer: C

3) 
public class TestString
{
public static void main(String[] args)
{
String str="420";
str+=42;
System.out.println(str);
}
}
What is the output?

A. 42
B. 420
C. 462
D. 42042
E. Compilation Error
F. An exception is thrown at run time

ANSWER: D

4) 
public class A extends B
{
public static void main(String[] args)
{
short myNum=7;
System.out.println(add(myNum,6));
}
}
class B
{
int add(int x,int y)

{
return x+y;
}
what is the result?
a. 13
b. Compilation fails due to multiple errors
c. Compilation fails due to an error on line 5
d. Compilation fails due to an error on line 9

Answer: C

5) 

Choose the correct one to define enum:

a. enum A{ ONE TWO THREE}
b. enum A{ ONE TWO THREE};
c. enum A{ ONE ,TWO,THREE};
d. None of the above

Answer:C



6) 
public static void main(String [] args)
{

try{
args=null;

args[0]="test";
System.out.println(args[0]);
}
catch(Exception ex)
{
System.out.println("Exception");
}
catch(NullPointerException npe)

{
System.out.println("Nullpointerexception");
}
}

What is the Result?

A. test
B. Exception
C. Compilation fail
D. NullPointerException

Answer: C

8) 

public class Test
{
static int[]a;

static 
{
a[0]=2;
}
public static void main(String[] args)
{}
}
Which exception or error will be thrown when a programmer attempt to run this code?

A. java.lang.StackOverflowError
B. java.lang.IllegalStateException
C. java.lang.ExceptionInInitializerError
D. java.lang.ArrayIndexOutOfBoundException

Answer: C

9) 
Which can appropriately be thrown by a programmer using Java SE technology to create a Desktop application?

A. ClassCastException
B. NullPointerException
C. NoClassDefFoundError
D. NumberFormatException
E. ArrayIndexOutOfBoundException

Answer: D

10) 
try{
int x=Integer.parseInt("two");

Which could be used to create an appropriate catch block?
A. ClassCastException
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionInInitializerError
F. ArrayIndexOutOfBoundException

Answer: C and D are correct. Integer.parseInt can throw a NumberFormatException and IllegalArgumentException is its superclass. But A,B,E and F are not in NumberFormatException's class hierarchy.

11)
Find the Correct Statement:

A. Cohesion is the OO principle most closely associated with hiding implementation details
B. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs
C. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single well-focused purpose
D. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types.

Answer:  C

12)
class Animal

{
public void eat()
{
System.out.println("Animal eating");
}
class Dog extends Animal
{
public void sniff();
System.out.println("Dog sniffing");
public void eat()
System.out.println("Dog eating");
}
}
public class Test
{
public static void main(String[] args)
{
new Test().run();
}
void run()

{
new Dog().eat();
((Animal)new Dog()).eat();
((Animal)new Dog()).sniff();
}
}

What is Result?

A. Animal eating Dog sniffing
B. Dog eating Dog sniffing
C. Error in Compilation at line 15
D. Error in Compilation at line 16

Answer: D

13) 
class A{
}
class B extends A
{}
public class Test
{
static String s="-";

public static void main(String[] args)
{
A[]aa=new A[2];

B[]bb=new B[2];
add(aa);
add(bb);
add(5);
System.out.println(s);
}
static void add(A[]...a2)

{
s+="4";
static void add(B[]...b1)
{
s+="3";
}
static void add(B[]b1)
{
s+="2";
}
static void add(Object o)
{
s+="1";
}
what is the Result?

A. compilation fails
B. -431
C. -421
D. -121

Answer: D

14) 

class Test
{
int[] cls={1,2,3,4,5,6,7,8,9,10};

}
public class College

{
public static void main(String[] args)

{
College[] c=new College[3];
c[0]=new College();
College co=new College();
c[1]=co;
co=null;
c[1]=null;
//TO DO
}
}
How many Objects will be created and after reaching at line 13,how many object will be eligible for GC

A. 5 created and 2 for GC
B. 3 created and 1 for GC
C. 3 created and 2 for GC
D. None

Answer: A

15) 

Choose the correct way to create a thread class:

A. public class MyThread extends Runnable
    {
      public  void run(){
     }}
B. public class MyThread extends Object
    {
      public  void run(){
     }}

C. public class MyThread implements Runnable
    {
      public  void run(){
     }}
D. public class MyThread extends Runnable
    {
       void run(){
     }}

Answer: C

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...