22
Question No :1 Read this piece of code carefully if("String".toString() == "String") System.out.println("Equal"); else System.out.println("Not Equal"); (Choose correct one from multiple below) 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. Not Compile 4. None correct is :1 Explanations :toString() method - This object (which is already a string!) is itself returned. The statement if("String".toString() == "String") compare the addresses. Both has pointing to same address. ------------------------------------------------------------------------------ Question No :2 Read this piece of code carefully if("String".trim() == "String") System.out.println("Equal"); else System.out.println("Not Equal"); (Choose correct one from multiple below) 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. Not Complile 4. None correct is :1 Explanations :trim() Returns a copy of the string, with leading and trailing whitespace omitted. This program compares address of "String" get from the String reference pool. Every new String checks in the String Reference pool, if already present then retun the address of the existing String otherwise create new one and add to String reference pool. ------------------------------------------------------------------------------ Question No :3 class C { public static void main(String[] args) { System.out.println(4+5+"String"); }} (Choose correct one from multiple below) 1. prints 9string 2. prints 45string 3. compile time error

Question No :1 - · PDF fileall classes of the java.lang ... Which of the following classes will not allow unsynchronized read operations by multiple threads? (Choose correct one from

Embed Size (px)

Citation preview

Question No :1

Read this piece of code carefully

if("String".toString() == "String")

System.out.println("Equal");

else

System.out.println("Not Equal");

(Choose correct one from multiple below)

1. the code will compile an print "Equal".

2. the code will compile an print "Not Equal".

3. Not Compile

4. None

correct is :1

Explanations :toString() method - This object (which is already a string!) is itself returned.

The statement if("String".toString() == "String") compare the addresses.

Both has pointing to same address.

------------------------------------------------------------------------------

Question No :2

Read this piece of code carefully

if("String".trim() == "String")

System.out.println("Equal");

else

System.out.println("Not Equal");

(Choose correct one from multiple below)

1. the code will compile an print "Equal".

2. the code will compile an print "Not Equal".

3. Not Complile

4. None

correct is :1

Explanations :trim() Returns a copy of the string, with leading and trailing whitespace omitted.

This program compares address of "String" get from the String reference pool.

Every new String checks in the String Reference pool, if already present then retun the address of

the existing String otherwise create new one and add to String reference pool.

------------------------------------------------------------------------------

Question No :3

class C {

public static void main(String[] args) {

System.out.println(4+5+"String");

}}

(Choose correct one from multiple below)

1. prints 9string

2. prints 45string

3. compile time error

4. Runtime exception

correct is :1

Explanations :arguments to the method are evalutated from left to right so 4+5+"string" ==>

9+string==>9string

------------------------------------------------------------------------------

Question No :4

class C {

public static void main ( String ka [ ] ) {

while ( false ) {

System.out.println ( "Value" ) ;

}

}

}

(Choose correct one from multiple below)

1. compile time error

2. prints Value infinitely

3. Runtime Exception

4. None of the above

correct is :1

Explanations :while ( false ) is Unreachable Code.

------------------------------------------------------------------------------

Question No :5

interface I{

final class C1 { //1

static int i=9; //2

}

}

class C2 implements I{

public static void main(String a[]){

System.out.println(I.C1.i); ///3

}}

(Choose correct one from multiple below)

1. compile time error at line 1

2. compile time error at line 2

3. compile time error at line 3

4. prints 9

correct is :4

Explanations :interfaces classes are by default static,final. so,no compile time errors are

genearated

------------------------------------------------------------------------------

Question No :6

The constructor for the Math class is private, so it cannot be instantiated

(Choose correct one from multiple below)

1. true

2. false

3. none of the above

4. none of the above

correct is :1

Explanations :Private is not visible to outside of class. So you can't create instance of Math class

out side of Math class.

------------------------------------------------------------------------------

Question No :7

class C {

static void m1(Object x) {System.out.print("Object");}

static void m1(String x) {System.out.print("String");}

public static void main(String[] args) {

m1(null);

}}

(Choose correct one from multiple below)

1. Prints Object

2. Prints String

3. compiletime error

4. None of the above

correct is :2

Explanations :The more specific of the two, m(String x), is chosen

------------------------------------------------------------------------------

Question No :8

all classes of the java.lang package are automatically imported

(Choose correct one from multiple below)

1. true

2. false

3. none of the above

4. none of the above

correct is :1

Explanations :java.lang package are automatically imported

------------------------------------------------------------------------------

Question No :9

class c1

{

public void m1()

{

System.out.println("m1 method in C1 class");

}

}

class c2

{

public c1 m1()

{

return new c1(){

public void m1()

{

System.out.println("m1 mehod in anonymous class");

}};}

public static void main(String a[])

{

c1 ob1 =new c2().m1();

ob1.m1();

}}

(Choose correct one from multiple below)

1. prints m1 method in C1 class

2. prints m1 method in anonumous class

3. compile time error

4. Runtime error

correct is :2

Explanations :the anonymous class overrides the m1 method in c1.so according to the dynamic

dispatch the m1 method in the anonymous is called

------------------------------------------------------------------------------

Question No :10

class C {

public static void main ( String ka [ ] ) {

Thread t = Thread . currentThread ( ) ;

t . setPriority ( - 1 ) ;

System . out . println ( " Done ! " ) ;

}

};

(Choose correct one from multiple below)

1. compile time error

2. Runtime Exception

3. The code compiles and runs fine

4. None of the above

correct is :2

Explanations :Code for setPriority is : if (newPriority greater then MAX_PRIORITY || newPriority

less then MIN_PRIORITY) {

throw new IllegalArgumentException();

}

where MAX_PRIORITY = 10 and MIN_PRIORITY =1 , so setPriority should not be less then 1 and

greater then 10.

------------------------------------------------------------------------------

Question No :11

abstract class A {} // 1

transient class B {} // 2

private class C {} // 3

static class D {} // 4

Which of these declarations will not produce a compile-time error?

(Choose correct one from multiple below)

1. 1

2. 2

3. 3

4. 4

correct is :1

Explanations :The modifiers, private and static, can be applied to a nested class, but can not be

applied to a class that is not nested transient is allowed only for variables.

------------------------------------------------------------------------------

Question No :12

class command {

public static void main (String[] a1) {

System.out.println(a1.length()); //1

System.out.println(a1[0]); //2

System.out.println(a1); //3

}}

(Choose correct one from multiple below)

1. compile time error at line1

2. compile time error at line2

3. compile time error at line3

4. Runtime exception

correct is :1

Explanations :length is not a method. it's a variable

------------------------------------------------------------------------------

Question No :13

abstract class vehicle{

abstract public void speed();

}

class car extends vehicle{

public static void main (String args[]) {

vehicle ob1;

ob1=new car(); //1

}}

(Choose correct one from multiple below)

1. compiletime error at line 1

2. forces the class car to be declared as abstract

3. Runtime Exception

4. None of the above

correct is :2

Explanations :Abstract method should be overriden otherwise Subclass should be abstract.

------------------------------------------------------------------------------

Question No :14

The Throwable class is the superclass of all exceptions in the Java language.

(Choose correct one from multiple below)

1. true

2. false

3. none of the above

4. none of the above

correct is :1

Explanations :Exception extends Throwable

------------------------------------------------------------------------------

Question No :15

interface I{

void f1(); // 1

public void f2(); // 2

protected void f3(); // 3

private void f4(); // 4

abstract void f5(); // 5

}

(Choose correct one from multiple below)

1. line 1,2,3,4

2. line 3,4

3. line 3

4. line 2,3,4

correct is :2

Explanations :all methods declared within an interface are implicitly public, a weaker access level

can not be declared

------------------------------------------------------------------------------

Question No :16

abstract class C1{

public void m1(){ //1

}}

abstract class C2{

public void m2(){ //2

}}

(Choose correct one from multiple below)

1. compile time error at line1

2. compile time error at line2

3. The code compiles fine

4. None of the above

correct is :3

Explanations :since the class C2 is abstract it can contain abstract methods

------------------------------------------------------------------------------

Question No :17

What is the signature of the run() method of the Runnable interface?

(Choose correct one from multiple below)

1. void run()

2. public void run(Runnable target)

3. public void run()

4. public static void run()

correct is :3

Explanations :public abstract void run();

------------------------------------------------------------------------------

Question No :18

When a byte is added to a char, what is the type of the result?

(Choose correct one from multiple below)

1. byte

2. int

3. long

4. non of the above

correct is :2

Explanations :The result of all arithmetic performed with the binary operators (not the assignment

operators) is an int, a long, a float, or a double. Here byte and char are promoted to int, so the

result is an int.

------------------------------------------------------------------------------

Question No :19

class A extends Thread {

public void run() {

System.out.print("A");

}

}

class B {

public static void main (String[] args) {

A a = new A();

a.start();

a.start(); // 1

}

}

(Choose correct one from multiple below)

1. compile time error

2. Runtime Exception

3. the code compile and runs fine

4. none of the above

correct is :2

Explanations :If the start method is invoked on a thread that is already running, then an

IllegalThreadStateException will probably be thrown

------------------------------------------------------------------------------

Question No :20

class C{

public static void main(String a[]) {

int i1=9;

int i2;

if(i1>3) {

i2=8;

}

System.out.println(i2);

}

}

(Choose correct one from multiple below)

1. compile time error

2. Runtim error

3. prints 8

4. prints 0

correct is :1

Explanations :since variable i1 is not final the value is not known at compiletime itself.so generate

compile time error

------------------------------------------------------------------------------

Question No :21

Which of the following classes will not allow unsynchronized read operations by multiple threads?

(Choose correct one from multiple below)

1. Vector

2. TreeMap

3. TreeSet

4. HashMap

correct is :1

Explanations :All the methods of Vector is synchronized.

------------------------------------------------------------------------------

Question No :22

if("String".replace('t','T') == "String".replace('t','T'))

System.out.println("Equal");

else

System.out.println("Not Equal");

(Choose correct one from multiple below)

1. will Print Equal

2. will Print Not Equal

3. compile time error

4. none of the above

correct is :2

Explanations :replace() method Returns a new string resulting from replacing all occurrences of

oldChar in this string with newChar.

So both the Strings are different and has different address.

------------------------------------------------------------------------------

Question No :23

System.out.println("String".substring(0,4));

This statement will Print ?

(Choose correct one from multiple below)

1. will print "Strin"

2. will print "Stri"

3. will cause compiler error

4. none of the above

correct is :2

Explanations :No Explanation Available

------------------------------------------------------------------------------

Question No :24

class base

{

base(int c)

{

System.out.println("base");

}

}

class Super extends base

{

Super()

{

System.out.println("super");

}

public static void main(String [] a)

{

base b1=new Super();

}

}

(Choose correct one from multiple below)

1. compile time error

2. runtime exceptione

3. the code compiles and runs fine

4. None of the above

correct is :1

Explanations :If super class has different constructor other then default then in the sub class you

can't use default constructor.

------------------------------------------------------------------------------

Question No :25

class C {

public static void main(String[] args) {

char c1=65;

switch(c1){

case 'A':

System.out.println("one");

default:

System.out.println("two");

case 'b':

System.out.println("three");

}}}

(Choose correct one from multiple below)

1. prints one twot hree

2. prints two three

3. compile time error

4. Runtime exception

correct is :1

Explanations :char is a legal value for switch clause

------------------------------------------------------------------------------

Question No :26

An abstract class must have at least one abstract method

(Choose correct one from multiple below)

1. true

2. true

3. none of the above

4. none of the above

correct is :2

Explanations :An abstract class without abstract methods is allowed

------------------------------------------------------------------------------

Question No :27

interface I{

int i; // line 1

}

class C implements I{

public static void main(String a[]){

System.out.println(i);

System.out.println(i++); //line 2

}

}

(Choose correct one from multiple below)

1. compile time error at line 1,2

2. compile time error at line 2

3. Runtime exception

4. Noneofthe above

correct is :1

Explanations :interface constants are final so,they must be initialized when declaring it and they

can not be altered

------------------------------------------------------------------------------

Question No :28

class C1{

public void m1(){ // 1

}

}

class C2 extends C1{ //2

private void m1(){

}

}

(Choose correct one from multiple below)

1. compile time error at line1

2. compile time error at line2

3. Runtime exception

4. None of the above

correct is :2

Explanations :extending to assign weaker access not allowed. Overridden method should be more

public.

------------------------------------------------------------------------------

Question No :29

class C{

public static void main (String[] args) {

byte b1=33; //1

b1++; //2

byte b2=55; //3

b2=b1+1; //4

System.out.println(b1+""+b2);

}}

(Choose correct one from multiple below)

1. compile time error at line 2

2. compile time error at line 4

3. prints 34,56

4. runtime exception

correct is :2

Explanations :b1+1 returns an integer value which can not be assigned to a byte variable

------------------------------------------------------------------------------

Question No :30

Which data type is wider for the purpose of casting: float or long?

(Choose correct one from multiple below)

1. float

2. long

3. none of the above

4. none of the above

correct is :1

Explanations :float is wider than long, because the entire range of long fits within the range of float.

------------------------------------------------------------------------------

Question No :31

class C{

public static void main (String[] args) {

String s1="hjhh"; // 1

String s2="\u0002"; //2

String s3="'\\'"; //3

}}

(Choose correct one from multiple below)

1. compile time error at line 1

2. compile time error at line 2

3. compile time error at line 3

4. the code runs without any error

correct is :4

Explanations :A String literal is a sequence of characters enclosed in double quotes

------------------------------------------------------------------------------

Question No :32

class bike

{

}

class arr extends bike{

public static void main(String[] args) {

arr[] a1=new arr[2];

bike[] a2;

a2=a1; //3

arr[] a3;

a3=a1; //5

}}

(Choose correct one from multiple below)

1. compile time error at line 3

2. compile time error at line 5

3. Runtime exception

4. The code runs fine

correct is :4

Explanations :bike is the superclass of arr.so they are compatible(superobject=subobject)

but subobject=superobject not allowed

------------------------------------------------------------------------------

Question No :33

class A extends Thread {

private int i;

public void run() {i = 1;}

public static void main(String[] args) {

A a = new A();

a.run();

System.out.print(a.i);

}}

(Choose correct one from multiple below)

1. Prints nothing

2. Prints: 1

3. Prints: 01

4. Compile-time error

correct is :2

Explanations :a.run() method was called instead of a.start(); so the full program runs as a single

thread so a.run() is guaranteed to complete

------------------------------------------------------------------------------

Question No :34

class C {

public static void main (String[] a1) {

System.out.print(a1[1] + a1[2] + a1[3]);

}

}

java command A B C

(Choose correct one from multiple below)

1. Prints: ABC

2. Prints BC and Runtime Exception

3. Prints: BCD

4. Runtime Exception

correct is :2

Explanations :array index outof bounds exception only till a1[2] is allowed.

------------------------------------------------------------------------------

Question No :35

class C{

static int s;

public static void main(String a[]){

C obj=new C();

obj.m1();

System.out.println(s);

}

void m1();

{

int x=1;

m2(x);

System.out.println(x+"");

}

void m2(int x){

x=x*2;

s=x;

}}

(Choose correct one from multiple below)

1. prints 1,2

2. prints 2,0

3. prints 2,2

4. compile time error

correct is :1

Explanations :Only objects and arrays are passed by reference.other are passed by value.s is a

static variable which is global to the class

------------------------------------------------------------------------------

Question No :36

class C {

public static void main(String[] args) {

int i1=1;

switch(i1){

case 1:

System.out.println("one");

case 2:

System.out.println("two");

case 3:

System.out.println("three");

}}}

(Choose correct one from multiple below)

1. prints one two three

2. prints one

3. compile time error

4. Runtime exceptionf

correct is :1

Explanations :There is no break statement in case 1 so it causes the below case statements to

execute regardless of their values

------------------------------------------------------------------------------

Question No :37

class C1

{

static interface I

{

static class C2

{

}

}

public static void main(String a[])

{

C1.I.C2 ob1=new C1.I.C2();

System.out.println("object created");

}

}

(Choose correct one from multiple below)

1. prints object created

2. Compile time error

3. Runtime Excepion

4. None of the above

correct is :1

Explanations :A static interface or class can contain static members.Static members can be

accessed without instantiating the particular class

------------------------------------------------------------------------------

Question No :38

Each element must be unique

Duplicate elements must not replace old elements.

Elements are not key/value pairs.

Accessing an element can be almost as fast as performing a similar operation on an array.

Which of these classes provide the specified features?

(Choose correct one from multiple below)

1. LinkedList

2. TreeMap

3. HashMap

4. HashSet

correct is :4

Explanations :element must be unique in Set

------------------------------------------------------------------------------

Question No :39

class C1

{

static class C2

{

static int i1;

}

public static void main(String a[])

{

System.out.println(C1.C2.i1);

}

}

(Choose correct one from multiple below)

1. prints 0

2. Compile time error

3. Runtime exception

4. None of the above

correct is :1

Explanations :static members can be accessed without instantiating the particular class

------------------------------------------------------------------------------

Question No :40

A signed data type has an equal number of non-zero positive and negative values available

(Choose correct one from multiple below)

1. true

2. false

3. none of the above

4. none of the above

correct is :2

Explanations :The range of negative numbers is greater by 1 than the range of positive numbers

------------------------------------------------------------------------------

Question No :41

class A extends Thread {

private int i;

public void run() {i = 1;}

public static void main(String[] args) {

A a = new A();

a.run();

System.out.print(a.i);

}}

(Choose correct one from multiple below)

1. Prints nothing

2. Prints: 1

3. Prints: 01

4. Compile-time error

correct is :2

Explanations :a.run() method was called instead of a.start(); so the full program runs as a single

thread so a.run() is guaranteed to complete

------------------------------------------------------------------------------

Question No :42

class C{

public static void main (String[] args) {

String s1="hjhh"; // 1

String s2="\u0002"; //2

String s3="'\\'"; //3

}}

(Choose correct one from multiple below)

1. compile time error at line 1

2. compile time error at line 2

3. compile time error at line 3

4. the code runs without any error

correct is :4

Explanations :A String literal is a sequence of characters enclosed in double quotes

------------------------------------------------------------------------------

Question No :43

class A extends Thread {

private int i;

public void run() {i = 1;}

public static void main(String[] args) {

A a = new A();

a.run();

System.out.print(a.i);

}}

How many threads are created in this Program?

(Choose correct one from multiple below)

1. 1

2. 2

3. 3

4. 0

correct is :1

Explanations :Main thread is a thread and calling run methods will not creat thread.

------------------------------------------------------------------------------

Question No :44

class H {

public static void main (String[] args) {

String s1 = "HHH";

StringBuffer sb2 = new StringBuffer(s1);

System.out.print(sb2.equals(s1) + "," + s1.equals(sb2));

}}

(Choose correct one from multiple below)

1. Prints: false,false

2. Prints: true,false

3. Prints: false,true

4. Prints: true,true

correct is :1

Explanations :s1 and sb2 are pointing to different object references

------------------------------------------------------------------------------

Question No :45

The relationship between a class and its superclass is

(Choose correct one from multiple below)

1. has-a

2. is -a

3. None of the above

4. none of the above

correct is :2

Explanations :has-a is composition relation and is-a is association relation.

------------------------------------------------------------------------------

Question No :46

String objects once created can not be modified

(Choose correct one from multiple below)

1. true

2. false

3. none of the above

4. none of the above

correct is :1

Explanations :Strings are constant; their values cannot be changed after they are created. String

buffers support mutable strings.

Because String objects are immutable they can be shared.

------------------------------------------------------------------------------

Question No :47

StringBuffer objects once created can not be modified

(Choose correct one from multiple below)

1. true

2. false

3. none of the above

4. none of the above

correct is :2

Explanations :Strings are constant; their values cannot be changed after they are created.

String buffers support mutable strings. Because String objects are immutable they can be shared.

------------------------------------------------------------------------------

Question No :48

class C {

public static void main(String[] args) {

int[]a1[]=new int[3][3]; //3

int a2[4]={3,4,5,6}; //4

int a2[5]; //5

}}

(Choose correct one from multiple below)

1. compiletime error at lines 3,4,5

2. compiltime error at line 4,5

3. compiletime error at line 3

4. Runtime Exception

correct is :2

Explanations :no value shoud be specified in the rightsidebrackets when constructing an array

------------------------------------------------------------------------------

Question No :49

interface I{

void f1(); // 1

public void f2(); // 2

protected void f3(); // 3

private void f4(); // 4

}

(Choose correct one from multiple below)

1. compiletime error at lines 1,2,3,4

2. compiletime error at line 3

3. compiletime error at line 1

4. compiletime error at lines 3,4

correct is :4

Explanations :all methods declared within an interface are implicitly public, a weaker access level

can not be declared.

------------------------------------------------------------------------------

Question No :50

class C{

int i;

public static void main (String[] args) {

int i; //1

private int a = 1; //2

protected int b = 1; //3

public int c = 1; //4

System.out.println(a+b+c); //5

}}

(Choose correct one from multiple below)

1. compiletime error at lines 1,2,3,4,5

2. compiletime error at lines 2,3,4,5

3. compiletime error at lines 2,3,4

4. prints 3

correct is :2

Explanations :The access modifiers public, protected and private, can not be applied to variables

declared inside methods.

------------------------------------------------------------------------------

Question No :51

class C{

static int s;

public static void main(String a[]){

C obj=new C();

obj.m1();

System.out.println(s);

}

void m1();

{

int x=1;

m2(x);

System.out.println(x+"");

}

void m2(int x){

x=x*2;

s=x;

}}

(Choose correct one from multiple below)

1. prints 1,2

2. prints 2,0

3. prints 2,2

4. compile time error

correct is :1

Explanations :Only objects and arrays are passed by reference.other are passed by value.s is a

static variable which is global to the class

------------------------------------------------------------------------------

Question No :52

What will happen when you attempt to compile and run the following code?

public class Inc{

public static void main(String argv[]){

Inc inc = new Inc();

int i =0;

inc.fermin(i);

i = i++;

System.out.println(i);

}

void fermin(int i){

i++;

}

}

(Choose correct one from multiple below)

1. Compile time error

2. Output of 2

3. Output of 1

4. Output of 0

correct is :4

Explanations :The method fermin only receives a copy of the variable i and any modifications to it

are not reflected in the version in the calling method. The post increment operator ++ effectivly

modifes the value of i after the initial value has been assiged to the left hand side of the equals

operator. This can be a very tricky conept to understand

------------------------------------------------------------------------------

Question No :53

What will happen when you attempt to compile and run the following code

class Base{

protected int i = 99;

}

public class Ab{

private int i=1;

public static void main(String argv[]){

Ab a = new Ab();

a.hallow();

}

abstract void hallow(){

System.out.println("Claines "+i);

}

}

(Choose correct one from multiple below)

1. Compile time error

2. Compilation and output of Claines 99

3. Compilation and output of Claines 1

4. Compilation and not output at runtime

correct is :1

Explanations :Abstract and native methods can't have a body: void hallow() abstract void hallow()

------------------------------------------------------------------------------