Professional Documents
Culture Documents
Java MockExam - 11
Java MockExam - 11
Java MockExam - 11
4)
JavaBeat Home SCJP 1.4 Home Objectives Forums Mock Exams Online Mock Exam Resources
Mock Exams
MockQuestions - 1 MockQuestions - 2 MockQuestions - 3 MockQuestions - 4
MockQuestions - 5 MockQuestions - 6 MockQuestions - 7 MockQuestions - 8
MockQuestions - 9 MockQuestions - 10 MockQuestions - 11 MockQuestions - 12
MockQuestions - 13 MockQuestions - 14 MockQuestions - 15 MockQuestions - 16
MockQuestions - 17 MockQuestions - 18 MockQuestions - 19 MockQuestions - 20
Answer : -------------------------
2 Any class may be unloaded when none of it's instances and class objects that represent this class are
reachable. True Or False?
(1) True
(2) False
Answer : -------------------------
class Test
{
public static void main(String[] args)
{
int d = 0;
try
{
int i = 1 / (d* doIt());
} catch (Exception e)
{
System.out.println(e);
}
}
public static int doIt() throws Exception
{
throw new Exception("Forget It");
}
}
True Or False?
(1) True
(2) False
Answer : -------------------------
4 What will be the result of attempting to compile and run the following program?
Answer : -------------------------
(1) True
(2) False
Answer : -------------------------
Answer : -------------------------
7 Given:
class TestClass
{
int i;
public TestClass(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return ""+i;
}
public static void main(String[ ] args)
{
TestClass t1 = new TestClass(0);
TestClass t2 = new TestClass(2);
System.out.println(t2);
System.out.println(""+t1);
}
}
Answer : -------------------------
Answer : -------------------------
9
What is the result of executing the following fragment of code:
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
Answer : -------------------------
10 Given two collection objects referenced by c1 and c2, which of these statements are true?
Select 2 correct options
Answer : -------------------------
class TechnoSample {
public static void main(String[] args) {
double d1 = 1.0;
double d2 = 0.0;
byte b =1;
d1 = d1/d2;
b = (byte) d1;
System.out.print(b);
}
}
Answer : -------------------------
12
Class finalization can be done by implementing the following method:
static void classFinalize() throws Throwable;
True Or False?
(1) True
(2) False
Answer : -------------------------
Options
14 Which of these statements concerning nested classes and interfaces are true?
Select 3 correct options
Answer : -------------------------
15 Given the following definition of class, which member variables are accessible from OUTSIDE the package
com.technopark?
package com.technopark;
Answer : -------------------------
(1) java.lang.String
(2) java.lang.Void
(3) java.lang.Null
(4) java.lang.Object
(5) None of the above
Answer : -------------------------
Answer : -------------------------
(1) 0
(2) 10
(3) null
(4) Compile error "sample.java:10: No variable i defined in class sample"
Answer : -------------------------
1] 4
Explanation:
These are the six facts:
1. Literal strings within the same class in the same package represent references to the same String object.
2. Literal strings within different classes in the same package represent references tothe same String object. (So
line 1 prints true)
3. Literal strings within different classes in different packages represent references to the same String object. (So
line 2 prints true)
4. Strings computed by constantexpressions are computed at compile time and then treated as if they were
literals. (So line 3 prints true)
5. Strings computed at run time are newly created and therefore are distinct. (So line 4 prints false)
6. The resultof explicitly interning a computed string is the same string as any pre-existing literal string with the
same contents. (So line 5 prints true) (credit: www.jdiscuss.com)
****
2] 2
Explanation:
A class or interface may be unloaded if and only if its class loader is unreachable (the definition of unreachable is
given in JLS 12.6.1). Classes loaded by the bootstrap loader are not unloaded. (credit: www.jdiscuss.com)
****
3] 2
Explanation:
It will print "Forget It" because before the division could take place doIt() throws an exception.
Java guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to
be fully evaluatedbefore any part of the operation itself is performed. If the binary operator is an integer division /
or integer remainder % , then its execution may raise an ArithmeticException, but this exception is thrown only
afterboth operands of the binary operator have been evaluated and only if these evaluations completed normally.
(credit: www.jdiscuss.com)
****
4] 3
Explanation:
Remember : variables are shadowed and methods are overridden. Which variable will be used depends on the
class that the variable is declared of. Which method will be used depends on the actual class of the object that is
referenced by thevariable. So, in line o1.m1(), the actual class of the object is C, so C's m1() will be used. So it
retruns 30. In line o2.i, o2 is declared to be of class B, so B's i is used. So it returns 20. (credit:
www.jdiscuss.com)
****
5] 2
Explanation:
First the value of str1 is evaluated (ie. one). Now, before the method is called the operands are evaluated, so str1
becomes "two". so "one".equals("two") is false.
****
6] 1,2,4
Explanation:
As an anonymous class has no name another class would have nothing to put after the extends keyword. A
constructor is a method with the same name as the class and no return type. If the class has no name there is no
name to give a constructormethod. (credit: www.examulator.com)
****
7] 4
Explanation:
The method print() if OutputStream takes an Object and prints out a string that is returned by calling toString()
on that object. Note that as toString() is defined in Object class, all objects in java have this method. So it prints 2
first.
The second object's toString() returns null, so it prints "null". There is no NullPointerException because no method
is called on null.
Now, the other feature of print/println methods is that if they get null as input parameter, they print "null". They
do not try to call toString() on null. So, if you have, Object o = null; System.out.println(o); will print null and will
not throw NullPointerException.
****
8] 5
Explanation:
NaN is unordered, so a numeric comparison operation involving one or two NaNs always returns false and any !=
comparison involving NaN returns true, including x != x when x is NaN.
****
9] 1
Explanation:
Note that, boolean operators have more precedence than =. (In fact, = has least precedenace) so, in (b2 != b1
= !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is
illegalbecause false is a value and not a variable!
Had it been something like (b2 = b1 != b2) then its valid because it will boil down to : b2 = false. Because all an if
() needs is a boolean, now b1 != b2 returns false which is a boolean andas b2 = false is an expression and every
expression has a return value (which is actually the LHS of the erpression). Here it returns true which is again a
boolean.
****
10] 4,5
Explanation:
public boolean retainAll(Collection c) retains only the elts in this collection that are contained in the specified
collection. In other words, removes from this collection all of its elts that are not contained in the specified
collection
public boolean removeAll(Collection c) removes all this collection's elts that are also contained in the specified
collection. After this call returns, this collection will contain no elts in common with the specifiedcollection
public boolean containsAll(Collection c) returns true if this collection contains all of the elts in the specified
collection
public boolean addAll(Collection c) adds all the elts in the specified collectionto this collection. The behavior of this
opern is undefined if the specified collection is modified while the opern is in progress(ie., the behavior of this call
is undefined if the specified collection is this collection, and is nonempty)
****
11] 4
Explanation:
1.0/0.0 results in Double.POSITIVE_INFINITY. Double.POSITIVE_INFINITY is converted to Integer.MAX_VALUE ('0'
followed by 31 '1's). Integer.MAX_VALUE is then cast to byte value, which simply takes the last 8 bits(11111111)
and is -1.
****
12] 2
Explanation:
PREVIOUSLY: If a class declares a class method classFinalize that takes no arguments and returns no result: static
void classFinalize() throws Throwable { . . . } then this method will be invoked before the class is unloaded . Like
thefinalize method for objects, this method will be automatically invoked only once. This method may optionally
be declared private, protected, or public.
NOW: Class finalization has been removed from the Java language. Thefunctionality of JLS 12.7 is subsumed by
instance finalization (JLS 12.6).Here is a rationale for this decision. http://java.sun.com/docs/books/jls/class-
finalization-rationale.html
Similar thing has happend toclass unloading: A class or interface may be unloaded if and only if its class loader is
unreachable (the definition of unreachable is given in JLS 12.6.1). Classes loaded by the bootstrap loader may not
be unloaded.
****
13] 2
Explanation:
(1) This may result in a deadlock (3) The is not necessary. Option 2 works just fine
****
14] 2,3,5
Explanation:
Inner class means a NON STATIC class defined inside a class. Top level nested class means a STATIC class defined
inside a class.
One can define a class as a static member of any top-level class. Classes which are static class members
andclasses which are package members are both called top-level classes. They differ from inner classes in a sense
that a top-level class can make direct use only of its own instance variables. The ability to nest classes in this way
allows anytop-level class to provide a package-like organization for a logically related group of secondary top-level
classes, all of which share full access to private members.(credit: www.jdiscuss.com)
****
15] 2,4
Explanation:
public > protected > package (ie. no modifier) > private where public is least restrictive
Remember:
protected is less restrictive than package access. So a method(or field) declared as protected will be accessible
from asubclass even if the subclass is not in the same package. The same is not true for package access.
A class can only have either public or no access modifier but a method or field can have all the four. Note that
static, final, nativeand synchronized are not considered as access modifiers
****
16] 5
Explanation:
Frequently it is necessary to represent a value of primitive type as if it were an object. There are following
wrapper classes for this purpose: Boolean, Byte, Character, Short, Integer, Long, Float, and Double.
Note that Byte,Short, Integer, Long, Float and Double extend from Number which is an abstract class. An object of
type Double, for example, contains a field whose type is double, representing that value in such a way that a
reference to it can be storedin a variable of reference type. These classes also provide a number of methods for
converting among primitive values, as well as supporting such standard methods as equals and hashCode
****
17] 1,5
Explanation:
A method can be overriden by defining a method with the same signature(i.e. name and parameter list) and
return type as the method in a superclass. Only methods that are accessible can be overriden. A private method
cannot therefore beoverriden in subclasses, but the subclasses are allowed to define a new method with exactly
the same signature. A final method cannot be overriden. An overriding method cannot exhibit behaviour that
contradicts the declaration of theoriginal method. An overriding method therefore cannot return a different type or
throw a wider spectrum of exceptions than the original method in the superclass
****
18] 4
Explanation:
(source: http://www.geocities.com/gnashes30/java/quest_bank/qb1.htm)
****