Passa: Class Public Static Void New
Passa: Class Public Static Void New
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
A. 12 15 B. 15 15
C. 345375 D. 375375
Answer: Option B
Explanation:
Output: 15 15
The reference variables a1 and a3 refer to the same long array object. When the[1] element
is updated in the fix() method, it is updating the array referred to bya1. The reference
variable a2 refers to the same array object.
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
Answer: Option B
Explanation:
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
A. slip stream
B. slipstream stream
Answer: Option D
Explanation:
class BitShift
{
public static void main(String [] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}
A. -2147483648 and 1
C. -2147483648 and -1
D. 1 and -2147483648
Answer: Option A
Explanation:
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The
bit transformation looks like this:
Option C is incorrect because the >>> operator zero fills the left bits, which in this case
changes the sign of x, as shown.
Option B is incorrect because the output method print() always displays integers in base 10.
Option D is incorrect because this is the reverse order of the two output numbers.
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
A. true
B. false
C. Compilation fails
Answer: Option C
Explanation:
The code will not compile because in line 7, the line will work only if we use (x==y)in the line.
The == operator compares values to produce a boolean, whereas the =operator assigns a
value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we
corrected this code, the output would be false.
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
A. small B. tiny
Answer: Option B
Explanation:
This is an example of a nested ternary operator. The second evaluation (x < 22)is true, so
the "tiny" value is assigned to sup.
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 52 B. 53
C. 63 D. 64
Answer: Option C
Explanation:
In the first two iterations x is incremented once and y is not because of the short
circuit && operator. In the third and forth iterations x and y are each incremented, and in the
fifth iteration x is doubly incremented and y is incremented.
View Answer Workspace Report Discuss in Forum
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 53 B. 82
C. 83 D. 85
Answer: Option B
Explanation:
class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}
A. 0 B. 7
C. 8 D. 14
Explanation:
The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9.
The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10.
The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
A. ok
B. dokey
C. ok dokey
D. No output is produced
E. Compilation error
Answer: Option B
Explanation:
class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}
void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo()
{
return "foo";
}
}
A. 9 7 7 foo 7 7foo
B. 72 34 34 foo34 34foo
C. 9 7 7 foo34 34foo
D. 72 7 34 foo34 7foo
Answer: Option D
Explanation:
Because all of these expressions use the + operator, there is no precedence to worry about
and all of the expressions will be evaluated from left to right. If either operand being evaluated
is a String, the + operator will concatenate the two operands; if both operands are numeric,
the + operator will add the two operands.
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
A. 77 B. 7 14
C. 14 0 D. 14 14
Answer: Option B
Explanation:
class Two
{
byte x;
}
class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
A. null null 42 B. 0 0 42
C. 0 42 42 D. 000
Answer: Option C
Explanation:
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
A. count = 0 B. count = 2
C. count = 3 D. count = 4
Explanation:
A. 2 B. 4
C. 8 D. 16
Answer: Option B
Explanation:
Java only ever passes arguments to a method by value (i.e. a copy of the variable) and never
by reference. Therefore the value of the variable i remains unchanged in the main method.
If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you had
16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If you had 128 right shifted by 2 bits
then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting to binary
to do the left and right bit shifts.
1. Which four options describe the correct default values for array elements of the types
indicated?
1. int -> 0
2. String -> "null"
3. Dog -> null
4. char -> '\u0000'
5. float -> 0.0f
6. boolean -> true
A. 1, 2, 3, 4 B. 1, 3, 4, 5
C. 2, 4, 5, 6 D. 3, 4, 5, 6
Answer: Option B
Explanation:
(2) is wrong because the default value for a String (and any other object reference) is null,
with no quotes.
(6) is wrong because the default value for boolean elements is false.
2. Which one of these lists contains only Java programming language keywords?
Answer: Option B
Explanation:
All the words in option B are among the 49 Java keywords. Although goto reserved as a
keyword in Java, goto is not used and has no function.
Option A is wrong because the keyword for the primitive int starts with a lowercase i.
Answer: Option D
Explanation:
Option B is wrong because it use something other than curly braces for the initialization.
Option C is wrong because it provides initial values for only one dimension, although the
declared array is a two-dimensional array.
A. method B. native
C. subclasses D. reference
E. array
Answer: Option B
Explanation:
Option A, D and E are not keywords. Option C is wrong because the keyword for subclassing in
Java is extends, not 'subclasses'.
A. interface B. string
C. Float D. unsigned
Answer: Option A
Explanation:
Option B is wrong because although "String" is a class type in Java, "string" is not a
keyword.
Option C is wrong because "Float" is a class type. The keyword for the Java primitive
is float.
A. 1, 2, 4 B. 2, 4, 5
Answer: Option A
Explanation:
(1), (2), and (4) are legal array declarations. With an array declaration, you can place
the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly
legal to split the brackets in a multidimensional array, and place them on both sides of
the identifier. Although coding this way would only annoy your fellow programmers, for
the exam, you need to know it's legal.
(3) and (5) are wrong because you can't declare an array with a size. The size is only
needed when the array is actually instantiated (and the JVM needs to know how much
space to allocate for the array, based on the type of array and the size).
View Answer Workspace Report Discuss in Forum
7.
public interface Foo
{
int k = 4; /* Line 3 */
}
1. final int k = 4;
2. public int k = 4;
3. static int k = 4;
4. abstract int k = 4;
5. volatile int k = 4;
6. protected int k = 4;
A. 1, 2 and 3 B. 2, 3 and 4
C. 3, 4 and 5 D. 4, 5 and 6
Answer: Option A
Explanation:
(1), (2) and (3) are correct. Interfaces can have constants, which are always
implicitly public, static, and final. Interface constant declarations
of public,static, and final are optional in any combination.
8. Which one of the following will declare an array and initialize it with five numbers?
B. int [] a = {23,22,21,20,19};
Answer: Option B
Explanation:
Option B is the legal way to declare and initialize an array with five elements.
Option D is wrong (and will not compile) because it declares an array with a size.
Arrays must never be given a size when declared.
1. char c1 = 064770;
2. char c2 = 'face';
3. char c3 = 0xbeef;
4. char c4 = \u0022;
5. char c5 = '\iface';
6. char c6 = '\uface';
A. 1, 2, 4
B. 1, 3, 6
C. 3, 5
D. 5 only
Answer: Option B
Explanation:
(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the
integer value 27128, which is legal because it fits into an unsigned 16-bit integer.char
c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits
into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of
a character.
char c2 = 'face'; is wrong because you can't put more than one character in a char
literal. The only other acceptable char literal that can go between single quotes is a
Unicode value, and Unicode literals must always start with a '\u'.
Answer: Option A
Explanation:
Member declarations in an interface disallow the use of some declaration modifiers; you
cannot use transient, volatile, or synchronized in a member declaration in an
interface. Also, you may not use the private and protected specifiers when declaring
members of an interface.
A. boolean b1 = 0;
B. boolean b2 = 'false';
C. boolean b3 = false;
D. boolean b4 = Boolean.false();
E. boolean b5 = no;
Answer: Option C
Explanation:
1. float f1 = -343;
2. float f2 = 3.14;
3. float f3 = 0x12345;
4. float f4 = 42e7;
5. float f5 = 2001.0D;
6. float f6 = 2.81F;
A. 1, 2, 4 B. 2, 3, 5
C. 1, 3, 6 D. 2, 4, 6
Answer: Option C
Explanation:
(1) and (3) are integer literals (32 bits), and integers can be legally assigned tofloats (also
32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather
than a double (the default for floating point literals).
A. String s1 = null;
B. String s2 = 'null';
Answer: Option A
Explanation:
Option A sets the String reference to null.
Option C is wrong because there are multiple characters between the single quotes ('abc').
C. 0 to 32767 D. 0 to 65535
Answer: Option D
Explanation:
A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535)
values.
A. Finally
B. Compilation fails.
Explanation:
If you put a finally block after a try and its associated catch blocks, then once execution enters
the try block, the code in that finally block will definitely be executed except in the following
circumstances:
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
A. finished B. Exception
Answer: Option C
Explanation:
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}
A. ABCD
B. Compilation fails.
Answer: Option C
Explanation:
Error is thrown but not recognised line(22) because the only catch attempts to catch
an Exception and Exception is not a superclass of Error. Therefore only the code in
the finally statement can be run before exiting with a runtime error (Exception in thread
"main" java.lang.Error).
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}
A. BD B. BCD
C. BDE D. BCDE
Answer: Option C
Explanation:
A Run time exception is thrown and caught in the catch statement on line 10. All the code
after the finally statement is run because the exception has been caught.
B. Compilation fails
Answer: Option D
Explanation:
A, B and C are incorrect based on the program logic described above. Remember that properly
handled exceptions do not cause the program to stop executing.
A. finally
B. exception finished
D. Compilation fails
Answer: Option C
Explanation:
(2) The exception cannot be assigned to the parameter of any catch clause of
thetry statement therefore the finally block is executed (line 9) and "finally" is output (line
11).
(4) The exception is propagated up the call stack and is caught by the catch in the main
method (line 20). This prints "exception".
(5) Lastly program execution continues, because the exception has been caught, and
"finished" is output (line 24).
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}
A. AC B. BC
C. ACD D. ABCD
Answer: Option C
Explanation:
There is no exception thrown, so all the code with the exception of the catch statement block
is run.
public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}
A. AB B. BC
C. ABC D. BCD
Answer & Explanation
Answer: Option D
Explanation:
(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never
executed.
(3) The exception is caught (line 10) and "B" is output (line 12)
(4) The finally block (line 14) is always executed and "C" is output (line 16).
(5) The exception was caught, so the program continues with line 18 and outputs "D".
A. Nothing. The program will not compile because no exceptions are specified.
B. Nothing. The program will not compile because no catch clauses are specified.
C. Hello world.
Answer: Option D
Explanation:
Finally clauses are always executed. The program will first execute the try block, printing
Hello world, and will then execute the finally block, printing Finally executing.
Option A, B, and C are incorrect based on the program logic described above. Remember that
either a catch or a finally statement must follow a try. Since the finally is present, the catch is
not required.
A. Ex0 caught
B. exception caught
Answer: Option A
Explanation:
An exception Exc1 is thrown and is caught by the catch statement on line 11. The code is
executed in this block. There is no finally block of code to execute.
1. Suppose that you would like to create an instance of a new Map that has an iteration order
that is the same as the iteration order of an existing instance of a Map. Which concrete
implementation of the Map interface should be used for the new instance?
A. TreeMap
B. HashMap
C. LinkedHashMap
Answer: Option C
Explanation:
The iteration order of a Collection is the order in which an iterator moves through the
elements of the Collection. The iteration order of a LinkedHashMap is determined by the
order in which elements are inserted.
The addAll method uses an iterator to the existing Collection to iterate through the elements of
the existing Collection and add each to the instance of the newLinkedHashMap.
Since the iteration order of the LinkedHashMap is determined by the order of insertion, the
iteration order of the new LinkedHashMap must be the same as the interation order of the
old Collection.
A. java.lang.String B. java.lang.Double
C. java.lang.StringBuffer D. java.lang.Character
Answer: Option C
Explanation:
java.lang.StringBuffer is the only class in the list that uses the default methods
provided by class Object.
3. Which collection class allows you to grow or shrink its size and provides indexed access to its
elements, but whose methods are not synchronized?
A. java.util.HashSet B. java.util.LinkedHashSet
C. java.util.List D. java.util.ArrayList
Answer: Option D
Explanation:
All of the collection classes allow you to grow or shrink the size of your
collection.ArrayList provides an index to its elements. The newer collection classes tend not
to have synchronized methods. Vector is an older implementation of ArrayListfunctionality
and has synchronized methods; it is slower than ArrayList.
4. You need to store elements in a collection that guarantees that no duplicates are stored and all
elements can be accessed in natural order. Which interface provides that capability?
A. java.util.Map B. java.util.Set
C. java.util.List D. java.util.Collection
Answer: Option B
Explanation:
Option B is correct. A set is a collection that contains no duplicate elements. The iterator
returns the elements in no particular order (unless this set is an instance of some class that
provides a guarantee). A map cannot contain duplicate keys but it may contain duplicate
values. List and Collection allow duplicate elements.
Option A is wrong. A map is an object that maps keys to values. A map cannot contain
duplicate keys; each key can map to at most one value. The Map interface provides three
collection views, which allow a map's contents to be viewed as a set of keys, collection of
values, or set of key-value mappings. The order of a map is defined as the order in which the
iterators on the map's collection views return their elements. Some map implementations, like
the TreeMap class, make specific guarantees as to their order (ascending key order); others,
like the HashMap class, do not (does not guarantee that the order will remain constant over
time).
Option C is wrong. A list is an ordered collection (also known as a sequence). The user of this
interface has precise control over where in the list each element is inserted. The user can
access elements by their integer index (position in the list), and search for elements in the list.
Unlike sets, lists typically allow duplicate elements.
Option D is wrong. A collection is also known as a sequence. The user of this interface has
precise control over where in the list each element is inserted. The user can access elements
by their integer index (position in the list), and search for elements in the list. Unlike sets, lists
typically allow duplicate elements.
A. Java.util.Map B. Java.util.List
C. Java.util.HashTable D. Java.util.Collection
Answer: Option A
Explanation:
6. Which interface provides the capability to store objects using a key-value pair?
A. Java.util.Map B. Java.util.Set
C. Java.util.List D. Java.util.Collection
Answer: Option A
Explanation:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map
to at most one value.
7. Which collection class allows you to associate its elements with key values, and allows you to
retrieve objects in FIFO (first-in, first-out) sequence?
A. java.util.ArrayList B. java.util.LinkedHashMap
C. java.util.HashMap D. java.util.TreeMap
Answer: Option B
Explanation:
LinkedHashMap is the collection class used for caching purposes. FIFO is another way to
indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use
the values() method and iterate over the resultant collection.
A. java.util.SortedMap B. java.util.TreeMap
C. java.util.TreeSet D. java.util.Hashtable
Answer: Option D
Explanation:
Hashtable is the only class listed that provides synchronized methods. If you need
synchronization great; otherwise, use HashMap, it's faster.
Answer: Option A
Explanation:
Option B is incorrect because any literal number with a decimal point u declare the computer
will implicitly cast to double unless you include "F or f"
Option D is incorrect because "d" tells the computer it is a double so therefore you are trying
to put a double value into a float variable i.e there might be a loss of precision.
10.
/* Missing Statement ? */
public class foo
{
public static void main(String[]args)throws Exception
{
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out,true);
out.println("Hello");
}
}
What line of code should replace the missing statement to make this program compile?
A. No statement required.
B. import java.io.*;
C. include java.io.*;
D. import java.io.PrintWriter;
Answer: Option A
Explanation:
The usual method for using/importing the java packages/classes is by using an import
statement at the top of your code. However it is possible to explicitly import the specific class
that you want to use as you use it which is shown in the code above. The disadvantage of this
however is that every time you create a new object you will have to use the class path in the
case "java.io" then the class name in the long run leading to a lot more typing.
A. 0 to 32767 B. 0 to 65535
Answer: Option B
Explanation:
The char type is integral but unsigned. The range of a variable of type char is from 0 to 216-
1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of
representing a wide range of international characters. If the most significant nine bits of
a char are 0, then the encoding is the same as seven-bit ASCII.
1. run
2. import
3. default
4. implement
A. 1 and 2 B. 2 and 3
C. 3 and 4 D. 2 and 4
Explanation:
1.
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */
}
A. after line 5
B. after line 6
C. after line 7
Answer: Option D
Explanation:
2.
class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
Where will be the most chance of the garbage collector being invoked?
A. After line 9
B. After line 10
C. After line 11
Answer: Option D
Explanation:
Option D is correct. Garbage collection takes place after the method has returned its reference
to the object. The method returns to line 6, there is no reference to store the return value. so
garbage collection takes place after line 6.
Option A is wrong. Because the reference to obj1 is stored in obj2[0]. The Objectobj1 still
exists on the heap and can be accessed by an active thread through the reference stored
in obj2[0].
Option B is wrong. Because it is only one of the references to the object obj1, the other
reference is maintained in obj2[0].
Option C is wrong. The garbage collector will not be called here because a reference to the
object is being maintained and returned in obj2[0].
3.
class Bar { }
class Test
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
public static void main (String args[])
{
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}
A. after line 12
B. after line 14
Answer: Option B
Explanation:
Option B is correct. All references to the Bar object created on line 6 are destroyed when a
new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore
the Bar object, created on line 6, is eligible for garbage collection after line 14.
Option A is wrong. This actually protects the object from garbage collection.
Option D is wrong. Not applicable because the object is eligible for garbage collection after line
14.
4.
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
A. After line 7
B. After line 8
C. After the start() method completes
D. When the instance running this code is made eligible for garbage collection.
Answer: Option D
Explanation:
Option A is wrong. The variable d is a member of the Test class and is never directly set to
null.
Option B is wrong. A copy of the variable d is set to null and not the actual variabled.
5.
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
A. 0 B. 1
C. 2 D. 3
Answer: Option B
Explanation:
By the time line 8 has run, the only object without a reference is the one generated as a result
of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by
the m1() method.
Ref: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
6.
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
Answer: Option C
Explanation:
Option A is wrong. This simply copies the object reference into the array.
Option B is wrong. The reference o is set to null, but, oa[0] still maintains the reference to
the Float object.
Option C is correct. The thread of execution will then not have access to the object.
7.
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible for garbage collection?
A. 0 B. 1
C. 2 D. 3
Explanation:
This is an example of the islands of isolated objects. By the time line 11 has run, the objects
instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of
them.
A. x.delete()
B. x.finalize()
C. Runtime.getRuntime().gc()
Answer: Option D
Explanation:
Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:
Option C is wrong. But it is interesting. The Runtime class has many methods, two of which
are:
1. getRuntime() - Returns the runtime object associated with the current Java
application.
2. gc() - Runs the garbage collector. Calling this method suggests that the Java virtual
machine expend effort toward recycling unused objects in order to make the memory
they currently occupy available for quick reuse. When control returns from the method
call, the virtual machine has made its best effort to recycle all discarded objects.
Interesting as this is, it doesn't destroy the object.
1. You want subclasses in any package to have access to members of a superclass. Which is the
most restrictive access that accomplishes this objective?
A. public B. private
C. protected D. transient
Answer: Option C
Explanation:
Access modifiers dictate which classes, not which instances, may access features.
Methods and variables are collectively known as members. Method and variable members are
given access control in exactly the same way.
default access is very similar to protected (make sure you spot the difference) default
access makes a member accessible only to classes in the same package.
public means that all other classes regardless of the package that they belong to, can access
the member (assuming the class itself is visible)
volatile indicates that a thread must reconcile its working copy of the field with the master
copy every time it accesses the variable.
After examining the above it should be obvious that the access modifier that provides the most
restrictions for methods to be accessed from the subclasses of the class from another package
is C - protected. A is also a contender but C is more restrictive, B would be the answer if the
constraint was the "same package" instead of "any package" in other words the subclasses
clause in the question eliminates default.
2.
public class Outer
{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }
Answer: Option A
Explanation:
Option B gives error - non-static variable cannot be referenced from a static context.
Option D gives error - non-static variable cannot be referenced from a static context.
3.
interface Base
{
boolean m1 ();
byte m2(short s);
}
A. 1 and 2 B. 2 and 3
C. 3 and 4 D. 1 and 5
Answer: Option C
Explanation:
(3) is correct because an abstract class doesn't have to implement any or all of its interface's
methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean).
(1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes
don't extend interfaces. (5) is incorrect because interface methods are implicitly public, so
the methods being implemented must be public.
1. public int a [ ]
2. static int [ ] a
3. public [ ] int a
4. private int a [3]
5. private int [3] a [ ]
6. public final int [ ] a
A. 1, 3, 4 B. 2, 4, 5
C. 1, 2, 6 D. 2, 5, 6
Answer: Option C
Explanation:
Option (3) is not a correct array declaration. The compiler complains with: illegal start of type.
The brackets are in the wrong place. The following would work:public int[ ] a
Option (4) is not a correct array declaration. The compiler complains with: ']' expected. A
closing bracket is expected in place of the 3. The following works:private int a []
Option (5) is not a correct array declaration. The compiler complains with 2 errors:
5.
public class Test { }
A. Test( ) B. Test(void)
Answer: Option C
Explanation:
Option A and B are wrong because they use the default access modifier and the access
modifier for the class is public (remember, the default constructor has the same access
modifier as the class).
Option D is wrong. The void makes the compiler think that this is a method specification - in
fact if it were a method specification the compiler would spit it out.
6. What is the most restrictive access modifier that will allow members of one class to have
access to members of another class in the same package?
A. public B. abstract
C. protected D. synchronized
E. default access
Explanation:
A. 1 and 3
B. 2 and 4
C. 1 only
Answer: Option D
Explanation:
Explanation:
Option B generates a compiler error: <identifier> expected. The compiler thinks you are trying
to create two arrays because there are two array initialisers to the right of the equals, whereas
your intention was to create one 3 x 3 two-dimensional array.
To correct the problem and make option B compile you need to add an extra pair of curly
brackets:
A. 1 and 2 B. 2, 3 and 5
C. 3, 4, and 5 D. 2 and 4
Answer: Option B
Explanation:
(2), (3), and (5). These are all valid interface method signatures.
10. You want a class to have access to members of another class in the same package. Which is
the most restrictive access that accomplishes this objective?
A. public B. private
Explanation:
The only two real contenders are C and D. Protected access Option C makes a member
accessible only to classes in the same package or subclass of the class. While default access
Option D makes a member accessible only to classes in the same package.
A. int B. byte
C. long D. double
Answer: Option D
Explanation:
However A, B and C are all wrong. Each of these would result in a narrowing conversion.
Whereas we want a widening conversion, therefore the only correct answer is D. Don't be put
off by the long cast, this applies only to the variable x and not the rest of the expression. It is
the variable y (of type double) that forces the widening conversion to double.
Answer: Option A
Explanation:
Option A is correct - because the class that extends A is just simply overridingmethod1.
Option B is wrong - because it can't override as there are less access privileges in the
subclass method1.
Option C is wrong - because to override it, the return type needs to be an integer. The
different return type means that the method is not overriding but the same argument list
means that the method is not overloading. Conflict - compile time error.
Option D is wrong - because you can't override a method and make it a class method i.e.
using static.
Answer: Option A
Explanation:
Option A is correct. It uses correct array declaration and correct array construction.
Option B is incorrect. It generates a compiler error: incompatible types because the array
variable declaration is not correct. The array construction expects a reference type, but it is
supplied with a primitive type in the declaration.
Option C is incorrect. It generates a compiler error: incompatible types because a string literal
is not assignable to a character type variable.
Option D is wrong, it generates a compiler error <identifier> expected. The compiler thinks
that you are trying to create two arrays because there are two array initialisers to the right of
the equals, whereas your intention was to create a 3 x 3 two-dimensional array.
14. Which two of the following are legal declarations for nonnested classes and interfaces?
A. 1 and 4 B. 2 and 5
C. 3 and 6 D. 4 and 6
Answer: Option C
Explanation:
15. Which of the following class level (nonlocal) variable declarations will not compile?
A. protected int a;
B. transient int b = 3;
Answer: Option C
Explanation:
A. 2, 4 B. 3, 5
C. 4, 5 D. 1, 2
Answer: Option D
Explanation:
(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong
type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f
= new float[3];
(2) causes a compiler error ( '{' expected ) because the array constructor does not specify the
number of elements in the array. The following is the correct syntax:float f2[ ] = new
float[3];
17. Given a method in a protected class, what access modifier do you use to restrict access to that
method to only the other members of the same class?
A. final B. static
C. private D. protected
E. volatile
Answer: Option C
Explanation:
Answer: Option A
Explanation:
1.
public void foo( boolean a, boolean b)
{
if( a )
{
System.out.println("A"); /* Line 5 */
}
else if(a && b) /* Line 7 */
{
System.out.println( "A && B");
}
else /* Line 11 */
{
if ( !b )
{
System.out.println( "notB") ;
}
else
{
System.out.println( "ELSE" ) ;
}
}
}
A. If a is true and b is true then the output is "A && B"
Answer: Option C
Explanation:
Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get
some chance of executing.
Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the
line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it
will always be trapped by the line 12 condition) therefore the output will never be "A && B".
Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the
line 5 output will be executed.
2.
switch(x)
{
default:
System.out.println("Hello");
}
1. byte
2. long
3. char
4. float
5. Short
6. Long
A. 1 and 3 B. 2 and 4
C. 3 and 5 D. 4 and 6
Answer: Option A
Explanation:
Switch statements are based on integer expressions and since both bytes and chars can
implicitly be widened to an integer, these can also be used. Also shorts can be
used. Short and Long are wrapper classes and reference types can not be used as variables.
3.
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
A. Compilation fails.
D. "odd" will be output for odd values of x, and "even" for even values.
Answer: Option A
Explanation:
The compiler will complain because of incompatible types (line 4), the if expects a boolean but
it gets an integer.
4.
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
Answer: Option D
Explanation:
Using the integer 1 in the while statement, or any other looping or conditional construct for
that matter, will result in a compiler error. This is old C Program syntax, not valid Java.
A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class
name). Line 8 is also valid because an equation may be placed in a String operation as shown.