Beruflich Dokumente
Kultur Dokumente
1. Which one of the fo llow ing w ill declare an array and initialize it w ith five numbers?
Option B is the legal w ay to declare and initialize an array w ith five e lements. Comments:
Option A is w rong because it show s an example of instantiating a class named Array, ...
passing the integer value 5 to the object's constructor. If you don't se e the brackets, yo u
can be certain there is no actual arra y o bject! In other wo rds, an Array object (instance
of class Array) is not the same as an array object.
Option C is w rong because it show s a legal array declaratio n, but w ith no initialization.
Option D is w rong (and w ill not compile) be cause it declares an array w ith a size. Arrays
must never be given a size w hen declared.
Take an Another Random Test!
Learn more problems on : Language Fundamentals
interface Count
{
short counte r = 0;
void countUp();
}
public class TestCount implements Count
{
public static void main(String [] args)
{
Te stCount t = new TestCount();
t.countUp();
}
public void countUp()
{
for (int x = 6; x>counter; x--, ++counter) /* Line 14 */
{
System.out.print(" " + counter);
}
}
}
A. 012 B. 123
indiabix.com/…/70 1/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
{
A( ) { }
}
class B extends A
{}
D. None of these.
Answer: Option B
Explanation:
Option B is correct. Class B inherits Class A's constructor w hich has no arguments.
Option A is w rong. Class B inherits Class A's constructor w hich uses de fault access.
A. Compilation fails.
D. "odd" w ill be output for odd values of x, and "even" fo r even values.
Answer: Option A
Explanation:
The compiler w ill complain because of incompatible types (line 4), the if expe cts a boolean
but it gets an integer. Don't be mislead by this question, it has nothing to do w ith
modulus (remainder o perator).
indiabix.com/…/70 2/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
Answer: Option C
Explanation:
Line 3 uses an assignment as opposed to compa rison. Because of this, the if statement
receives an inte ger value instead of a boolean. And so the compilation fails.
A. 0 B. 2
C. 4 D. 6
Answer: Option D
Explanation:
Because there are no break statements, the program gets to the default case and adds
2 to j, the n goes to case 0 and adds 4 to the new j. The result is j = 6.
int I = 0;
label:
if (I < 2) {
System.out.print("I is " + I);
I++;
continue label;
}
A. I is 0
B. I is 0 I is 1
C. Compilation fails.
Answer: Option C
Explanation:
indiabix.com/…/70 3/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}
A. Ex0 caught
B. exception caught
Answer: Option A
Explanation:
An exception Exc1 is throw n and is caught by the ca tch statement on line 11. The code is
executed in this block. There is no finally block of code to execute.
A. No code is necessary.
B. throw s Exception
C. catch ( Exception e )
D. throw s RuntimeExceptio n
Answer: Option B
Explanation:
Option B is correct. This w orks because it DOES thro w an exception if an error occurs.
Option A is w rong. If you compile the code as given the co mpiler w ill complain:
"unreported exception must be caught or decla red to be throw n" The class extends
Exception so w e are forced to test for exceptions.
Option C is w rong. The catch statement belongs in a method body not a metho d
indiabix.com/…/70 4/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
B. HashMap
C. LinkedHashMap
Answer: Option C
Explanation:
The ite ration order of a Collection is the order in w hich an iterator moves through the
elements of the Collection. The iteration order of a LinkedHashMap is determined by the
order in w hich 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 new
LinkedHashMap.
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. 42
B. Runtime Exception
Answer: Option A
Explanation:
This co de is an example of an ano nymous inner class. They can be declared to extend
ano ther class or implement a single inte rface. Since they have no name you can not use
the "new" ke yw ord on them.
In this case the annoyno us class is e xtending the Object class. Within the {} you place
the methods you w ant for that class. After this class has been declare d its methods can
be use d by that object in the usual w ay e.g. objectname.annoymousClassMethod()
12. What is the name of the method used to start a thread execution?
indiabix.com/…/70 5/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
Learn more problems on : Threads
A. run(); B. construct();
C. start(); D. register();
Answer: Option C
Explanation:
Option C is correct. The start() method causes this thre ad to begin execution; the Java
Virtual Ma chine calls the run method of this threa d.
Option A is w rong. The run() method of a thread is like the main() method to an
application. Starting the thread causes the object's run method to be called in that
separately executing thread.
Answer: Option D
indiabix.com/…/70 6/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
C. After line 11
Answer: Option D
Explanation:
Option D is correct. Garbage collection takes place a fter 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 w rong. Because the reference to obj1 is sto red in obj2[0]. The Object obj1 still
exists on the heap and can be accessed by an active thread through the reference
stored in obj2[0].
Option B is w rong. Because it is only one of the references to the o bject obj1, the other
reference is maintained in obj2[0].
Option C is w rong. The garbage colle cto r w ill not be called here because a reference to
the object is being maintained and returned in obj2[0].
When is the Demo object, created on line 4, eligible for garbage collection?
A. After line 7
B. After line 8
D. When the instance running this code is made eligible for garbage collection.
Answer: Option D
Explanation:
Option A is w rong. The variable d is a member of the Test class and is never directly set
to null.
Option B is w rong. A copy of the variable d is set to null and not the actual variable d.
Option C is w rong. The variable d exists outside the start() method (it is a class member).
So, when the start() method finishes the variable d still ho lds a reference.
indiabix.com/…/70 7/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
after line 11 runs, how many objects are eligible for garbage collection?
A. 0 B. 1
C. 2 D. 3
Answer: Option C
Explanation:
This is an example of the islands of isolated objects. By the time line 11 has run, the
objects insta ntiated in lines 6 and 7 are referring to each other, but no live thread can
reach either of them.
switch (z)
{
case 4: System.out.println("4 ");
case 5: System.out.println("5 ");
default: assert z < 10;
}
if ( z < 10 )
assert z > 4: z++; /* Line 22 */
System.out.println(z);
}
}
A. Line 11
B. Line 12
C. Line 14
D. Line 22
Answer: Option D
Explanation:
Assert statements should not cause side effects. Line 22 changes the value of z if the
assert statement is false.
Option B is fine because it is perfectly acceptable to call a method w ith the second
indiabix.com/…/70 8/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
9/22/2010 Online Java Programming …
B. app
C. apea
D. apep
Answer: Option B
Explanation:
Both substring() and charAt() methods are indexed w ith a zero-base, and substring()
returns a String of length arg2 - arg1.
A. z
B. xz
C. yz
D. Compilation fails.
Answer: Option D
Explanation:
Submit Test
indiabix.com/…/70 9/9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)