Java is the future !: Quiz of the day
Showing posts with label Quiz of the day. Show all posts
Showing posts with label Quiz of the day. Show all posts

Tuesday, August 16, 2011

Quiz of the day | OCPJP - Aug 16, 2011


What will be the result of compiling and running the following code:

Options :
a) true
true
b) false
true
c) false
false
d) None of the above



Solution: Correct choice is option B.
Creating a new instance of the class File, you're not yet making an actual file, you're just creating a filename. So file.exists() return false.
createNewFile() method created an actual file. So file.exists() return true.

Quiz of the day | OCPJP - 16 Aug 2011


What is the output for the above code ?

Options :
a) Compile with error - Not allowed to override the return type of a method with a subtype of the original type.
b) class A - return C
c) class B - return D
d) Runtime Exception

Friday, December 3, 2010

Quiz of the Day | December 3, 2010

What will be the output when you compile and execute the following program? 
Choose one answer. 
a. Prints true 
b. Prints false 
c. Prints 9 
d. Prints 2 
e. Compiler error : Incorrect declaration of main method 
f. None of these


Solution : Choice F is the correct answer. 

The code does not compile because the && operator works only with boolean operands. If both operands are true, the && operator returns true and false in all the other cases. In this case, integer operands arepassed to the && operator, so the code does not compile. Since the code does not compile, choices A to D are incorrect.

Choice E is incorrect because there is nothing wrong with the declaration of the main method. The modifiers public and static can appear in any order. Also, the main method takes a String[] as the argument.


Source : "Facebook Community"

Sunday, November 7, 2010

Quiz of the Day | November 7, 2010



What will happen when you attempt to compile and run the above code? (Assume that the code is compiled and run with assertions enabled.)

a. It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd). 
b. It will print odd and even numbers from 0 to 9 incorrectly (0 odd and 1 even). 
c. Compilation error at line 9. 
d. Compilation error at line 10. 
e. It will result in an infinite loop.


Solution : Choice A is the correct answer.

The code will compile successfully and it will print odd and even numbers from 0 to 9 correctly, without reversing even and odd.

Let's look at the execution of the code. The assert statement at line 9 asserts that "i%2==0" must be true, which will be true for all the even numbers in the range 0 to 9 (0, 2, 4, 6, and 8), and false for all the odd numbers in the range 0 to 9 (1, 3, 5, 7, and 9). When the assert statement is evaluated to true, the second expression on the right hand side of the colon is not evaluated. On the other hand, when the assert statement is evaluated to false, the expression on the right hand side of the colon is evaluated and an AssertionError is thrown.

When the for loop starts with i=0, the assert condition at line 9 is evaluated to true, hence the other expression i-- is not evaluated, the value of i (0 here) is printed at line 10. Next, i is incremented to 1 in the next iteration of the for loop. The assert condition at line 9 is false in this case, hence the expression i--is evaluated (i becomes 0) and an AssertionError is thrown. The print statement at line 10 is not executed in this case. The catch statement at line 12 catches this error and the print statement at line 14 increments i with ++i and prints its value (which is 1 in this case). The same process is repeated for all the numbers, thus effectively printing all the even and odd number correctly.

The key here lies in understanding when the right-hand side expression of an assert statement gets evaluated. It gets evaluated *only* when the boolean left-hand side expression is evaluated to false. Hence, the loop will not result in an infinite loop. 


Source : "Facebook community"

Saturday, November 6, 2010

Quiz of the Day | November 6, 2010

What value is printed out by executing the above code?

a. 2.7 
b. 1.7 
c. 0.0 
d. -1.0


Solution : Choice A is the correct answer.

In Java, all parameters are passed by value. In case of primitives, the copy of the variable is passed, while in case of object references, it's the copy of the reference that is passed. 

When the argument is a primitive type, pass-by-value means that the method can change the value of the passed argument (in the method scope) but, the called method cannot change the value of the variable in the calling method.

When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.

In the example above, d is passed as value, hence the change done in the doMinus() method doesn't reflect on d outside the doMinus() method. Thus, the value printed is 2.7


Source : "Facebook community"

Friday, November 5, 2010

Quiz of the Day | November 5, 2010 [2]

What will be printed on standard output if the following class is executed using the command "java Test 1 two 3" ?

a. 1 

b. two 
c. NumberFormatException 
d. ArrayIndexOutOfBoundsException 
e. Code does not compile


Solution : Choice C is the correct answer. 

In Java, command line arguments are stored in the args array which is an argument to the main method. The index of the args array starts with 0 and the first command line argument is the word after the classname. Thus, args[0] will be 1 and args[1] will be "two".

The parseInt() method of the Integer class parses the String argument to the int type. The String passed to the parseInt() method should have only digits, with an exception that it can start with '-' to indicate a negative number. Otherwise, NumberFormatException will be thrown. Thus, when "two" is passed to the parseInt() method, NumberFormatException will be thrown.

Please note that there is nothing wrong with the definition of main, the static word can come after or before public keyword. 


Source : "Facebook community"

Quiz of the Day | November 5, 2010

What will be the result of compiling the following programs and executing it using the following command - java Child?


a. Prints 9
b. Compiler error at line 5
c. Compiler error at line 6
d. None of the above



Solution :  Choice C is the correct answer.


You cannot access the protected variable in the Parent class from the Child class using the parent class reference. The Child class does inherit the variable x, but it can be accessed only by an object reference of the Child class. There is no error at line 5. It is legal to refer to a class as packagename.classname even if the package is already imported. So choice B is incorrect.

Source : "Facebook community"

Thursday, November 4, 2010

Quiz of the Day | November 4, 2010

Given the code below, which access modifiers (public, protected, or private) can legally be placed before the myMethod() method on line 3, if no other changes are made to the code? If line 3 is left as it is, which keywords can legally be placed before the myMethod method on line 8?

a. private or nothing (default) on line 3. Nothing (default) or protected or public on line 8. 
b. public or protected on line 3. private or nothing (default) on line 8. 
c. Nothing (default) or protected or public on line 3. private or nothing (default) on line 8. 
d. public on line 3 and private on line8.


Solution : Choice A is the correct answer.

The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly (default access / no modifier), it can only be private or friendly in the superclass. Secondly, if the method in superclass is left as it is (i.e., friendly access), the method in subclass can be friendly, protected, or public. 

Source : "Facebook community"

Wednesday, November 3, 2010

Quiz of the Day | November 3, 2010


What will be the result of running the following code? 
a.) The output: Value is - Hello
b.) The output: Value is - Hello world
c.) A compiler error
d.) No output
e.) An exception


Solution : Choice B is the correct answer.

Since the OR operator used here is not the short-circuit operator (which is "||"), both operands are evaluated even though the first operanditself is true which guarantees that the whole condition is true. Thus the value of the StringBuffer instance s after the evaluation of the boolean condition is "Hello world" and hence choice B is correct.

Please note here that if we had used "||" operator instead of "|", the correct answer would have been choice A, because in that case the right-hand side operand wouldn't have been evaluated as the left-hand side operand would have been true. 


Source : "Facebook community"

Quiz of the Day | November 2, 2010


What will happen when we try to compile the following code?


a. The compiler objects to line 3 comparing an Object with an array. 
b. The compiler objects to line 5 casting an Object to an array of int primitives. 
c. The compiler objects to line 11 comparing an Object to an array of Objects. 
d. It compiles without error.


Solution : Choice D is the correct answer. 

The "instanceof" operator tests the class of an object at runtime. It returns true if the type of the left-hand argument is the same as, or is some subtype of, the class or interface specified by the right-hand operand. Arrays are also objects, so they can be referred to by variables of type Object. Hence comparing Object type to array using instanceof is allowed. So choices A, B and C are incorrect.

Source : "Facebook community"

Monday, November 1, 2010

Quiz of the Day | November 1, 2010


Which of the following statements about the following code fragment are true?

a.) The code compiles successfully and displays "Test".
b.) Changing the code at line 13 to "public void print(int x)" will allow the code to compile correctly.
c.) The code fails to compile. The compiler complains about duplicate methods.
d.) Changing the return type on line 13 from 'void' to 'int' will allow the code to compile correctly.


Solution : Choices B and C are the correct answers.

The code appears to be attempting to overload the print() method.

However, the only differentiating feature between the methods is the static keyword. This is insufficient for overloading. Therefore, choice C is correct and choice A is incorrect because the code fails to compile due to duplicate methods.

Choice B is correct because the methods are different overloaded versions of the print() methods.

Choice D is incorrect because the return parameters are not sufficient to differentiate between the overloaded methods.

More about overloading is available at http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.9


Source : "Facebook community"

Sunday, October 31, 2010

Quiz of the Day | Oct 31, 2010



How many objects will be eligible for garbage collection after the execution of line 9 in the following code?
a.) 8
b.) 14
c.) 15
d.) 16


Solution : This code creates 16 objects of type Double. Out of which, 15 Double objects are created by the iterations of the nested for loops at lines 6 and 7, and one more Double object is created at line 5. The very first iteration of the loop makes the object created at line 5 eligible for garbage collection. With each iteration of the loop, a new object is created and assigned to 'x', and the old (or previously created) object becomes eligible for garbage collection. Only the most recently created object (for i=4 and j=2) is not eligible for garbage collection.

Thus, out of the created 16 objects (including the one created at line 5), a total of 15 objects have become eligible for garbage collection after the execution of line 9.

Actually, as explained in the earlier paragraph, each iteration of the loop (line 8) makes the previously created object eligible for garbage collection. Thus, by the time both nested loops finish their execution (before execution of line 9), a total of 15 objects have become eligible for garbage collection. The execution of line 9 itself does not affect garbage collection in any way. Want to know more?

You can read more about garbage collection at -
http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html
http://www.developer.com/tech/article.php/628881


Source : "Facebook community"

Saturday, October 30, 2010

Quiz of the Day | Oct 30, 2010

Which of the following statements are true about the relationships between the following classes?
a.) A Bar is a Baz
b.) A Foo has a Bar
c.) A Baz is a Foo 
d.) A Foo is a Baz
e.) A Baz has a Bar


Solution : Choices B, C, and E are the correct answers. An instance of the Baz class is also an instance of the class Foo since the Baz class extends the Foo class. So choice C is correct. 

A Baz has a Bar, since instances of the Baz class contain an instance of the Bar class by reference. So choice E is correct. 

A Foo has a Baz, since instances of the Foo class contain an instance of the Baz class by reference. Since a Foo has a Baz, which in turn has a Bar, a Foo has a Bar. So choice B is also correct. 

Choice A is incorrect because Bar does not extend Baz. Choice D is also incorrect because Foo does not extend Baz either. Refer http://cnx.rice.edu/content/m11709/latest/ for more details.


Source : "Facebook community"

Friday, October 29, 2010

Quiz of the Day | Oct 29, 2010

Which of the following statements are true?

a. As soon as the reference count for an object reaches zero, it will be immediately garbage collected.
b. The finalize() method will not be invoked more than once by the JVM on the same object.
c. The finalize() method cannot be overloaded.
d. The garbage collection implementation is JVM-dependent.
e. If the finalize() method is overridden, a call to the super class's finalize() method is inserted automatically by the compiler.


Solution : Choices B and D are the correct answers.

As specified by the Java API documentation, the finalize() method is never invoked more than once by a Java virtual machine for any given object. Also, the garbage collection indeed is implementation-dependent. This is what the Java Virtual Machine Specification (JVMS) has to say about memory management -

"The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements."

Choice A is incorrect because when the reference count for an object reaches zero, it becomes eligible for garbage collection but that *does not* necessarily mean that it will be immediately garbage collected.

It is perfectly legal to overload the finalize() method like any other method. However, the JVM will always invoke the default, no arguments finalize() method. Hence choice C is incorrect.

Choice E is incorrect because if you override the finalize() method, you must explicitly call the superclass' finalize() method. The compiler *does not* automatically insert this call. Want to know more?

You can read more about garbage collection at -http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html
Also, the API documentation of the Object class contains the necessary information about the finalize() method -
http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html


Source : "Facebook community"

Thursday, October 28, 2010

Quiz of the Day | Oct 28, 2010

Given the following two classes, which statement is true?

Choose one answer. 
a. The code will compile and run only if line 5 is removed. 
b. The code will compile and run only if line 6 is removed 
c. The code compiles and runs, printing "Test" twice. 
d. The code compiles but throws an exception at runtime.


Solution : Choice B is the correct answer.

The code will compile and run only if line 6 is removed. 

Subclasses that are in a package different from that of the superclass can access protected members of the superclass only through inheritance. Such a subclass cannot use a superclass reference to access a protected member.

Line 5 is legal since we are invoking the test() method using the subclass reference itself, it is the inherited method which we are accessing.

However, line 6 will not compile because the subclass is using the superclass reference to call the test() method.

Since the code does not compile, choices C and D are incorrect.

Reference:
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html


Source : "Facebook community"