 |
|
Solutions to All Questions |
[1] 1
Explanation:
Option 1 is correct because a class must be declared abstract when it contains one or more abstract
methods. Abstract methods are methods without a body.
Options 2, 3, and 4 are incorrect because compilation succeeds.
* * * *
[2] 1,2,3
Explanation:
Options 1, 2, and 3 are correct because a switch statement test requires an integer expression. If
the values in the expression are of type byte, short, or char, promotion to the integer value will
occur.
Options 4 and 5 are incorrect because class references, floating point values, and long values are
not allowed in the expression.
* * * *
[3] 4
Explanation:
Option 4 is correct because no accessible references to the object created at line 3 exist after the
method returns. This is because automatic variables are used for all the references in this code.Option 1 is incorrect because the NewGarb class compiles successfully. Option 2 is incorrect
because the getIt() method may or may not be declared static. Option 3 is incorrect because an
exception is not received.
* * * *
[4] 4,5
Explanation:
Option 4 is correct because "char c = 'f';" placed on line 4 will create a local variable, which is a
variable declared within a method. These variables exist only as long as the method exists.
Option 5 is correct because "static char c;" placed on line 2 will create a class variable, which is a
variable declared within the class. These variables are created and destroyed as the class is created
and destroyed.
Option 1 is incorrect because it does not use the static keyword. A variable that is accessed by a
static method must be declared static, or the access must be made using an instance reference.Likewise, option 3 is not declared static using the static keyword. Option 2 is incorrect because it
does not initialize the variable. Option 6 is incorrect because char is not a class, and so cannot be
instantiated.
* * * *
[5] 2
Explanation:
Option 2 is correct because t will result in a value of 4. The ++ operator will increment variables by
one. Stepping through the for loop until p < 5, incrementing the variables, and performing the
calculation will result in t being equal to 4.
* * * *
[6] 3
Explanation:
Option 3 is correct because the method name, argument list, and return type are the same as the
getType method defined in the IntType class. Overriding methods must be declared with the same
name, argument list, and return type as the overridden method.
Option 1 is incorrect because the return type is not the same as the getType method in the IntType
class. Option 2 is incorrect because both the return type and argument list are different from the
getType method in the IntType class. Option 4 is incorrect because the argument list is different
from the getType method in the IntType class.
* * * *
[7] 5
Explanation:
Option 5 is correct because the Runnable interface is implemented by declaring a synchronized
run() method. The method is declared as synchronized to signify that the object lock must be
obtained
Options 1, 2, and 3 are incorrect because compilation succeeds. Option 4 is incorrect, but would
be correct if the run() method were not declared as synchronized
* * * *
[8] 3
Explanation:
Option 3 is correct because it creates the Birthdays class and displays the Birthdays frame. The
frame is displayed by calling the setVisible() method with an argument of true.
Options 1, 2, and 4 are incorrect because the Birthdays class is not created and the frame is not
displayed.
* * * *
[9] 3
Explanation:
Option 3 is correct because it uses the ceil() method. This method rounds a value up (to ceiling).
Option 1 is incorrect because the min() method returns the smaller of two values. Option 2 is
incorrect because the max() method returns the larger of two values. Option 4 is incorrect because
the floor() method rounds a value down.
* * * *
[10] 3
Explanation:
Option 3 is correct because the put() method is used to add a key/value association to a Map.
Option 1 is incorrect because the values() method returns a Collection of all values in a Map.Option 2 is incorrect because the entrySet() method returns a Set of all mappings in a Map. Option 4 is
incorrect because the pubAll() method copies all mappings from one Map to another.
* * * *
[11] 1,3,5
Explanation:
1, 3, and 4 are correct. In each of these answers, the argument list differs from the original, so the method is an overload.Overloaded methods are effectively independent, and there are no constraints on the accessibility, return type, or exceptions that may be thrown.2 would be a legal overriding method, except that it cannot be defined in the same class as the original method; rather, it must be declared in a subclass.4 is also an override, since the types of its arguments are the same: Changing the parameter names is not sufficient to count as overloading.
* * * *
[12] 2,4
Explanation:
2 and 4 are correct. 1 is illegal because it is less accessible than the original method; the fact that it throws no exceptions is perfectly acceptable. 2 is legal because it overloads the method of the parent class,and as such it is not constrained by any rules governing its return value, accessibility, or argument list. The exception thrown by 3 is sufficient to make that method illegal. 4 is legal because the accessibility and return type are identical,and the method is an override because the types of the arguments are identical--remember that the names of the arguments are irrelevant.The absence of an exception list in 4 is not a problem: An overriding method may legitimately throw fewer exceptions than its original, but it may not throw more.
* * * *
[13] 5,6
Explanation:
5 and 6 are correct. The Cat class is a subclass of the Pet class, and as such should extend Pet, rather than containing an instance of Pet. 2 and 3 should be members of the Pet class and as such are inherited into the Cat class; therefore, they should not be declared in the Cat class. 4 would declare a reference to an instance of the Cat class, which is not generally appropriate inside the Cat class itself(unless, perhaps, you were asked to give the Cat a member that refers to its mother). Finally, the neutered flag and markings descriptions, 5 and 6, are the items called for by the specification; these are correct items
* * * *
[14] public class Cat extends Pet
Explanation:
public class Cat extends Pet
. The class should be public since it is to be used freely throughout the application. The statement "A cat is a pet" tells us that the Cat class should subclass Pet. The other words offered are required for the body of the definitions of either Cat or Pet--for use as member variables--but are not part of the opening declaration.
* * * *
[15] 3
Explanation:
3 is correct. The first message is produced by the Base class when b1.method(5) is called and is therefore Value is 5. Despite variable b2 being declared as being of the Base class, the behavior that results when method() is invoked upon it is the behavior associated with class of the actual object, not with the type of the variable. Since the object is of class Sub, not of class Base, the second message is generated by line 3 of class Sub: This value is 6.
* * * *
[16] 2,3
Explanation:
2 and 3 are correct. Since the class has explicit constructors defined, the default constructor is suppressed, so 1 is not possible. 2 and 3 have argument lists that match the constructors defined at lines 2 and 4 respectively,and so are correct constructions. 4 has three integer arguments, but there are no constructors that take three arguments of any kind in the Test class, so 4 is incorrect.Finally, 5 is a syntax used for construction of inner classes and is therefore wrong.
* * * *
[17] 1,3
Explanation:
1 and 3 are correct. In the constructor at lines 2 and 3, there is no explicit call to either this() or super(), which means that the compiler will generate a call to the zero argument superclass constructor, as in 1. The explicit call to super() at line 5 requires that the Base class must have a 7.constructor as in 3. This has two consequences. First, 3 must be one of the required constructors and therefore one of the answers.Second, the Base class must have at least that constructor defined explicitly, so the default constructor is not generated, but must be added explicitly. Therefore the constructor of 1 is also required and must be a correct answer.At no point in the Test class is there a call to either a superclass constructor with one or three arguments, so 2 and 4 need not explicitly exist.
* * * *
[18] 1,2,5
Explanation:
1, 2, and 5 are correct. Inner classes may be defined with any accessibility, so private is entirely acceptable and 1 is correct. Similarly, the static modifier is permitted on an inner class, which causes it not to be associated with any particularinstance of the outer class. This means that 2 is also correct. Inner classes defined in methods may be anonymous--and indeed often are--but this is not required, so 3 is wrong. 4 is wrong because it is not possible for an inner class defined in a method to access the local variables of the method, except for those variables that are marked as final. Constructing an instance of a static inner class does not need an instance of the enclosing object, but all non-static inner classes do require such a reference, and that reference must be available to the new operation. The reference to the enclosing object is commonly implied as this, which is why it is commonly not explicit. These points make 5 true.
* * * *
[19] 1,2,3,5
Explanation:
1, 2, 3, and 5 are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore 1 and 2 are correct, despite the fact that b is marked private.Variables in the enclosing method are only accessible if those variables are marked final, so the method argument c is correct, but the variable d is not.Finally, the parameter e is of course accessible, since it is a parameter to the method containing line 8 itself.
* * * *
[20] 1
Explanation:
1 is correct. Construction of a normal inner class requires an instance of the enclosing class. Often this enclosing instance is provided via the implied this reference, but an explicit reference may be used in front of the new operator, as shown in 1.
Hence 2 is illegal--it actually attempts to instantiate the interface ActionListener as if that interface were itself an inner class inside Outer.
3 is illegal since Inner is a non-static inner class, and so it requires a reference to anenclosing instance when it is constructed. The form shown suggests the implied this reference, but since the method is static, there is no this reference and the construction is illegal.
4 is illegal since it attempts to use argumentsto the constructor of an anonymous inner class that implements an interface. The clue is in the attempt to define a constructor at line 3. This would be a constructor for the interface MyInterface not for the inner class--this is wrong on two counts.
* * * *
[21] 3
Explanation:
Method K.m1 creates five objects of type J. Each instance has a name represented by an integer between 0 and 4 inclusive. If the garbage collector does not run then the program will produce no output.If the garbage collector does run then the output of the program could be a series of integers that are the names of four of the five objects. As each new object is created its reference is assigned to the reference variable j.The previously referenced object then becomes eligible for garbage collection. The last object created, 4, is not available for garbage collection until method m1 runs to completion.The while loop in the synchronized block will never complete because J.notFinalized will never return zero. This program is intended to provide a working example of garbage collecting objects referenced by local variables.
* * * *
[22] 4,5
Explanation:
Variable z is not declared, thus, z cannot be resolved on lines 2 and 3. In Java, z cannot be declared that way. In order to get this code to compile, we have to write either:
int z=1,x=z;
Or
int z=1;
int x=z;
Or
int z=1;
int x=1;
* * * *
[23] 4
Explanation:
First off, 017 is an octal integer literal having the decimal value 15. Second, the cast to byte only applies to j and not to j >> 2 as a whole. Thus, j is downcast to byte and then upcast to int again before the shifting.Briefly, the cast has no effect here. Then, the binary sequence of 15 is 00000000 00000000 00000000 00001111, which, shifted 2 bits to the right, yields
00000000 00000000 00000000 00000011. Finally, the binary sequence, 11, is printed. Note that the prefixed 0's are dismissed.
* * * *
[24] 1,4,5
Explanation:
The garbage collection thread is a deamon thread. The latter die when there are no more users threads running. The garbage collection cannot be forced.
* * * *
[25] 5
Explanation:
The code does not compile because no IOException is thrown when invoking myref.test(). Note that myref's declared and runtime types are TechnoSampleSuband thus no dynamic lookup will be performed. However, if you change the declared type to TechnoSample, the code will compile and the correct answer would be A because method test() isoverridden in TechnoSampleSub
* * * *
[26] 3,5
Explanation:
Do not mix b!=b and b=!b. In the former, we check if b's value is different from b's value (?!) which is clearly false. In the latter, we assign b's opposite value to itself, that is, if b is true, then after b=!b, b ends up being false.Moreover, be aware that b=i==2 is evaluated as b=(i==2) because operator = has the lowest precedence. Finally, note that the arguments to the Exception constructor are evaluatedfrom the left to the right. First, b!=b is evaluated, then i=args.length (args.length is 2, so i keeps its value), and finally, b=i==2.
* * * *
[27] 1,4
Explanation:
First, pay attention to the class hierarchy (B and C are sibling classes!!) Then, there is no such thing as forward-referencing issues when using interfaces declared later in the compilation unit.On line 4, we are dealing with an object whose runtime type is D which implements interface E. The cast is mandatory, though, since the reference type (A) is not assignmentcompatible with the reference type E. The cast on line 5 is mandatory for the same reasons.
* * * *
[28] 4
Explanation:
We declare an array of Object of length two. We then initialize each element to a new Object. We have 2 objects in the array and the array itself (which is an object, too!), that makes 3.
* * * *
[29] 2
Explanation:
The code does not compile because i (declared in the try block!) is not in scope when accessed from the catch block.
* * * *
[30] 2
Explanation:
The code does not compile because the anonymous inner class (new Runnable(){...}) tries to access the non-final local variable i
* * * *
[31] 4
Explanation:
Whenever you perform some operation upon a String object, you will get a new String object if the operation had some effect on the original String, we say that a String is immutable and that its sequence of characters cannot be changed.Here, we are dealing with a StringBuffer which is an implementation of a mutable String, that is you can modify the character sequence it contains. Whenever you perform an operation upon a StringBuffer, the same StringBuffer(whose content has been modified) is returned. The next important thing to remember is that when a reference to an object is passed as argument to a method, a copy of the reference is actually passed to the method. In this case,buf1 is passed to addSomeStuff and replace is invoked upon it. The changes operated by replace are affecting buf1. We then store the reference to buf (which references the same object as buf1 into a local StringBuffer called b.
* * * *
[32] 3,4
Explanation:
This question mainly tests your knowledge of the various wrapper classes' constructors. The most notorious thing here is that we can actually give a double as an argument to the Float constructor.
Otherwise, the question tests your ability to understand how Collection classes work. The effect of add(), elementAt() and setElementAt() is trivial, just have a look at the API of the Vector class for more information
* * * *
[33] 3,5
Explanation:
This shows you that Unicode characters are processed very early during the lexical parsing of a Java source file. Here LF, which is a line terminator, isactually translated to a line terminator and this results in malformed Java code. For instance, the example given above is actually interpreted as:
String s = "Hello
World";
You can notice that the first line is unterminated and the compiler lets you know that.
* * * *
[34] 2,3
Explanation:
An ArithmeticException is always thrown when executing this code because Math.random() returns a double between 0.00000 and 0.999999 and Math.floor() just removes the decimal part (the part after the dot).So, the result of the combination of Math.floor(Math.random()) is always 0.0.The cast to int guarantees that 0 will be returned, and an integer division by 0 does always throw an ArithmeticException.
* * * *
[35] 1,2
Explanation:
We wait on Thread t1 to finish before printing End but none of the threads are started so only End is printed. Moreover, the same output will be printed whichever the platform is.
* * * *
[36] 1,4
Explanation:
Line 1 does not compile because getPrimitive() takes a byte and we pass it an int. In a normal assignment (byte b = 127;) this would work because 127 is in the range for byte values and the compiler implicitely does a norrowing conversion.But this is not the case in method invocations. A quote from JLS 5.3: "The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matchingresolution process". This speaks for itself. Line 3 compiles fine because we have to do with a widening primitive conversion form short to int which is perfectly straightforward.
* * * *
[37] 2,3,4
Explanation:
Overriding is for non-static methods and hiding is for static methods. So the following statements are the only true statements about hiding and overriding:
- a static method (in a subclass) may hide another static method (in a superclass)
- a static method (in a subclass) cannot hide a non-static method (in a superclass)
- a non-static method (in a subclass) may override another non-static method (in a superclass)
- a non-static method (in a subclass) cannot override a static method (in a superclass)
* * * *
[38] 1,3,4
Explanation:
myref is an instance of class TechnoSampleSub referenced by a variable of type TechnoSample. Method test() in class TechnoSample is overridden in class TechnoSampleSub, thus the one to be invoked is the one declared in class TechnoSampleSub(Polymorphism!). Moreover, test() has no obligation to declare a throws clause (see overriding rules!). The try-catch block is mandatory because myref could as well reference an instanceof class TechnoSample and in that case the method test() to be invoked would be the one declared in class TechnoSample which throws an exception.
* * * *
[39] 2
Explanation:
1) is false because we know that the data in d2 was changed. 3) is false because we know that the data in d1 was not changed. The names d1 and d2 are used in both main and method to be confusing. They are different and stored on the stack in different plaAll the interesting stuff that happen in the Example class is in method. main simply initializes some data and prints the results. In method, the followning happens:
1.d2 has its year set to 100 (really 2000, as 2.Object d1 is set to be the same as d2. This is a change of the actual reference, not in the data at d1.
Both of these line are perfectly legal, and do not result in a compilation error, so d) is false. I will also point out here that e) is
String context. toString() is defined by the Object class and so it is available on all classes in Java. Most non-trivial classes override toString() to return more explicit information about themselves.
* * * *
[40] 1
Explanation:
* * * *
[41] 1,2,3
Explanation:
Because the variable oak is declared as static only one copy of it will exist. Thus it can be changed either through the name of its class or through the name of any instance of that class.Because it is created as an integer it canot be assigned a fractional component without a cast.
* * * *
[42] 4
Explanation:
The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method.The post increment operator ++ effectivly modifes the value of i after the initial
value has been assiged to the left hand side of the equals operator.
* * * *
[43] 1,3,4
Explanation:
The options for this question might look suspiciously easy if you are not aware of the effects of the post-increment operators. The ++ and -- operations for examples 1 and 4 only come into effect after the outputoperations, ie after whatever else is done to them on that line of code. Option 2 should be fairly obvious as you should know that the single quote characters indicate a char value, ie storing the character ratherthan the numberical value for 0.
* * * *
[44] 1
Explanation:
A top leve (non nested) class cannot be private.
* * * *
[45] 3
Explanation:
he default layout manager for a Frame is the BorderLayout manager. This Layout manager defaults to placing components in the centre if no constraint is passed with the call to the add method.
* * * *
[46] 4
Explanation:
If the for loop were defined in a method called:public void run()
and the call in the main method had been to b.start(), the list of values from 0 to 9 would have been output.
* * * *
[47] 4
Explanation:
Note that && is short-circuit logical AND operator. If first operand (before &&) is false (or evaluated to false) then the other operand will not be evaluated.This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false.If you change the value of b1 to true, processing occurs as you would expect and the output would be "We are equal 20".
* * * *
[48] 1
Explanation:
When we pass references in Java what actually gets passed is the value of that reference (i.e. memory address of the object being referenced and not the actual object called by that reference) and it gets passed as value (i.e a copy of thereference is made). Now when we make changes to the object referenced by that reference it reflects on that object even outside of the method being called but any changes made to the reference itself is not reflected on that reference outside ofthe method which is called.
* * * *
[49] 1
Explanation:
The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly (default modifier) it can only be private or friendly in the superclass.Also, if the method in superclass is left as it is (i.e. friendly access) the method in subclass can be friendly, protected or public.
* * * *
[50] 1
Explanation:
Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() bechanged to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur.
* * * *
2001-2003 Technopark. Developed by: Giri Mandalika