Sunteți pe pagina 1din 9

9/22/2010 Online Java Programming …

Java Programming Test 10

Java Programming Result & Statistics

1. Which one of the fo llow ing w ill declare an array and initialize it w ith five numbers?

A. Array a = new Array(5); Total number of questions


Number of answ ered questions
B. int [] a = {23,22,21,20,19};
Number of unanswered
questions
C. int a [] = new int(5);

D. int [5] array;


Feedback

Answer: Option B Quality of the Test

Explanation: Difficulty of the Test

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

Discuss about this problem : Discuss in Forum

2. What w ill be the output o f the program?

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
{}

Which statement is true?

A. Class B'S constructor is public.

B. Class B'S constructor ha s no arguments.

C. Class B'S constructor includes a call to this( ).

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.

Option C is w rong. There is just no implicit call to this( ).

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

4. public void test(int x)


{
int odd = x % 2;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}

Which statement is true?

A. Compilation fails.

B. "odd" w ill alw ays be output.

C. "even" w ill alw ays be output.

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).

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

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.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

6. What w ill be the output o f the program?

public class Test


{
public static void main(String args[])
{
int i = 1, j = 0;
switch(i)
{
case 2: j += 6;
case 4: j += 1;
default: j += 2;
case 0: j += 4;
}
System.out.println("j = " + j);
}
}

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.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

7. What w ill be the output o f the program?

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.

D. None of the above

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

C. Compilation fails because of an error at line 2.

D. Compilation fails because of an error at line 9.

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.

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum

9. public class Exce ptionTest


{
class TestException extends Exception {}
public void runTest() throw s TestException {}
public void test() /* Point X */
{
runTest();
}
}

At Point X on line 5, w hich code is necessary to make the code compile?

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

D. The answ er depends on the implementation of the existing instance.

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.

W hen a new LinkedHashMap is created by passing a reference to an existing Collection to


the constructor of a LinkedHashMap the Collection.addAll method w ill ultimately be invoked.

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.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

11. What w ill be the output o f the program?

public static void main(String[] args)


{
Object obj = new Object()
{
public int hashCode()
{
return 42;
}
};
System.out.println(obj.hashCode());
};

A. 42

B. Runtime Exception

C. Compile Error at line 2

D. Compile Error at line 5

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()

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

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

Discuss about this problem : Discuss in Forum

13. Which method registers a thread in a thread scheduler?

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.

Option B is w rong. There is no construct() method in the Thread class.

Option D is w rong. There is no register() method in the Thread class.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

14. What w ill be the output o f the program?

public class ThreadTest extends Thread


{
public void run()
{
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String []a rgv)
{
(new Threa dTest()).start();
}
}

A. The code fails to compile in the main() method

B. The code fails to compile in the run() method

C. Only the text "In run" w ill be displayed

D. The text "In run" follow ed by "Leaving run" w ill be displayed

Answer: Option D

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

15. class HappyGarbage01


{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();

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

D. Garba ge collector never invoked in methodA()

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].

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum

16. class Test


{
priva te Demo d;
void start()
{
d = new Demo();
this.takeDe mo (d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}

When is the Demo object, created on line 4, eligible for garbage collection?

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 D is correct. By a pro cess of elimination.

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.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum

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.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum

18. public class Test2


{
public static int x;
public static int foo (int y)
{
return y * 2;
}
public static void main(String [] args)
{
int z = 5;
assert z > 0; /* Line 11 */
assert z > 2: foo(z); /* Line 12 */
if ( z < 7 )
assert z > 4; /* Line 14 */

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);
}
}

w hich line is an exa mple of an inappropriate use of assertions?

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 A is fine; a second expression in an assert statement is not required.

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.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

20. What w ill be the output o f the program?

public class BoolTest


{
public static void main(String [] args)
{
Boolean b1 = new Boolean("false");
boolean b2;
b2 = b1.booleanValue();
if (!b2)
{
b2 = true;
System.out.print("x ");
}
if (b1 & b2) /* Line 13 */
{
System.out.print("y ");
}
System.out.println("z");
}
}

A. z

B. xz

C. yz

D. Compilation fails.

Answer: Option D

Explanation:

The compiler fails at line 13 because b1 is a reference variable to a Boolean w rapper


object, not a bo ole an primitive. Logical boo lea n tests can't be made on Boolean objects.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

Submit Test

indiabix.com/…/70 9/9

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

S-ar putea să vă placă și