Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 "";
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






This post first appeared on Learnprogramingbyluckysir, please read the originial post: here

Share the post

SCJP Written Test Questions

×

Subscribe to Learnprogramingbyluckysir

Get updates delivered right to your inbox!

Thank you for your subscription

×