100% found this document useful (1 vote)
272 views

SCJP Garbage Collection

This document contains 10 questions and answers about Java concepts like garbage collection, operators, assignments, and data types. It discusses the results of various operations using operators like <<, &, |, ^ and evaluates boolean expressions. Key points covered include the order of operations, implicit type conversions during assignments, and valid casting between primitive types.

Uploaded by

V
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC or read online on Scribd
100% found this document useful (1 vote)
272 views

SCJP Garbage Collection

This document contains 10 questions and answers about Java concepts like garbage collection, operators, assignments, and data types. It discusses the results of various operations using operators like <<, &, |, ^ and evaluates boolean expressions. Key points covered include the order of operations, implicit type conversions during assignments, and valid casting between primitive types.

Uploaded by

V
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC or read online on Scribd
You are on page 1/ 4

SCJP Garbage collection, Operators and assignments

By: admin - Tuesday, September 9th, 2008 at 10:56 am

QUESTION : 1

What is the result that will be printed out here ?


int i = - 128;
System.out.println((byte)i << 1);

1. 0
2. -256
3. 127
4. -127
5. -1

ANS : 2

Though the value in i is cast to a byte


it reverts to an int as the result of the left shift
which makes the value of i - 256

QUESTION : 2

What is the result of the following operation?


int n = 8;
n = n & n + 1;
n<<=n/2;

1. 32
2. 12
3. 8
4. 24
5. 128
6. 0

ANS : 5

The arithmetic + operator takes precedence over the


bitwise & operator so 8 & 9 = 8
Shifting 8 by four bits to the left gives you 128

QUESTION : 3

Observe the code below


public class Test {
int a[] = new int[4];
void aMethod()
{
int b = 0 , index;
a[a[b]] = a[b] = b = 2;
index = ??? ;
System.out.println(a[index]);
}
}
What value assigned to the variable index will print a non zero
result?

1. 0
2. 1
3. 2
4. 3

ANS : 1

the operands are evaluated from left to right here


so in the expression a[a[b]] the value of b is 0
and a[0] also holds the value 0 when the expression
is evaluated so the array element at index 0 is assigned
the value 2.

QUESTION : 4

Will this test evaluate to true?


Object a = "hello";
String b = "hello";
if(a == b)
System.out.println("equal");
else
System.out.println("not equal");

1. Yes
2. No

ANS : 1

It does not matter if the object reference is of type Object


this will still return true because both of them point to the
same String object in the string pool.

QUESTION : 5

True or False.
The garbage collector is required to makes sure that all objects
held by soft references are garbage collected before the VM throws
an OutOfMemoryError.

1. True
2. False

ANS : 1

If the VM finds itself unable to allocate memory for


a new object it is required to kick start the garbage
collector and reclaim all objects that are not held
by strong references before throwing an OutOfMemoryError.

QUESTION : 6

What will be the value of the variable x here


int x = 8 | 12 ^ 8;
1. 4
2. 8
3. 12
4. 0

ANS : 3

QUESTION : 7

What will be the value of the variable x here


int x = 8 ^ 5 & 6;

1. 0
2. 4
3. 6
4. 8
5. 12

ANS : 5

The precedence of bitwise operators


is AND , XOR and OR
so 5 AND 6 is evaluated first which gives you 4.
8 XOR 4 = 12

QUESTION : 8

What is the result that will be printed out


public class Test {
public static void main(String[] args)
{
int a = -8;
int b = ~ -33;
a>>>=b;
System.out.println(a);
}
}

1. 1
2. 8
3. -8
4. 0
5. 16

ANS : 3

QUESTION : 9

Is this legal ?

public class Test {


public static void main(String[] args)
{
char c = 65;
double d = 10;
if(c==d)
System.out.println("equal");
else
System.out.println("not equal");
}
}

1. No.
2. Yes.

ANS : 2

QUESTION : 10

Select the valid assignments

1.int i = (int)16.2d;
2. byte b = (byte)(long)16.2;
3. byte b = 128;
4. float f = 16.2;
5. byte b = (int)16.2;

ANS : 1,2,5

You might also like