|
QUESTION1
You have the following code in a file called Test.java
class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
public class Test extends Base{}
What will happen if you try to compile and run this?
1. It will fail to compile.
2. Runtime error
3. Compiles and runs with no output.
4. Compiles and runs printing "Hello"
ANS : 4
Correct answer/s : 4
This will compile and print "Hello"
The entry point for a standalone java program is
the main method of the class that is being run.
The java runtime system will look for that method
in class Test and find that it does have such a method.
It does not matter whether it is defined in the class itself
or is inherited from a parent class.
QUESTION2
What is the result of trying to compile and run the following code.
public final static void main(String[] args){
double d = 10.0 / -0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
1. output Positive infinity
2. output Negative infinity
3. Will fail to compile
4. Runtime exception
ANS : 1
Corr answer : 1
There is no such thing as a positive or negative zero.
Hence the result is always positive infinity.
QUESTION3
What is the result that will be printed out ?
void aMethod()
{
float f = (1 / 4) * 10;
int i = Math.round(f);
System.out.println(i);
}
1. 2
2. 0
3. 3
4. 2.5
5. 25
ANS : 2
Corr Answer : 2
The result of 1/4 will be zero because integer
divion is carried out on the operands.
If you need to obtain a fractional value
you need to use either a float or double literal
as in 1F / 4F.
QUESTION4
Which of the following are valid declarations?
Note : None of the literals used here
contain the character O they are all zeroes.
1. int i = 0XCAFE;
2. boolean b = 0;
3. char c = 'A';
4. byte b = 128;
5. char c = "A";
ANS : 1,3
Correct answer/s :1,3
1. is correct as it is a valid hexadecimal number.2. is wrong
because you can only assign the values true and false to them
4 is wrong because 128 is beyond the range of a byte. 5is wrong
because "A" is not a char it is a String.
QUESTION5
What is the result of trying to compile and run this program.
public class Test{
public static void main(String[] args){
int[] a = {1};
Test t = new Test();
t.increment(a);
System.out.println(a[a.length - 1]);
}
void increment(int[] i){
i[i.length - 1]++;
}
}
1. Compiler error.
2. Compiles and runs printing out 2
3. Compiles and runs printing out 1
4. An ArrayIndexOutOfBounds Exception at runtime
ANS : 2
Correct answer/s : 2
You are passing a reference to an array as
the argument to the method. The method may not
modify the passed object reference but it can modify
the object itself.
QUESTION6
What will happen if you try to compile and run this ?
public class Test{
static{
print(10);
}
static void print(int x){
System.out.println(x);
System.exit(0);
}
}
1. Compiler error.
2. Will throw a NoSuchMethod error at runtime.
3. It will compile and run printing out "10"
4. It will run with no output.
5. It will run and print "10" and then crash with an error.
ANS : 3
Correct answer : 3
This will run, print a message and terminate gracefully.
The runtime system needs to load the class before it can look
for the main method. So the static initializer will run first
and print "10". Immediately after that System.exit(0) will be called
terminating the program before an error can be thrown.
QUESTION7
Is this legal?
long longArr[];
int intArr[] = { 7 ,8 , 9};
longArr = intArr;
1. Yes
2. No
ANS : 2
Correct answer : 2
You cannot assign a reference to an array of primitives
to another unless they contain the same primitive types.
QUESTION8
True or False.
The range of a byte is from -127 to 128
1. True
2. False
ANS : 2
Correct answer/s : 2
The statement is false. The range of an array
is from - 128 to 127
QUESTION9
Identify the valid assignments.
1. float f = \u0038;
2. long L2 = 2L;
3. float f = 1.2;
4. char c = '/u004E';
5. byte b = 100;
ANS : 1,2,4,5
Corr answer : 1, 2, 5.
1 is correct because \u0038 is unicode for nbr 8.
3 is wrong because 1.2 is a double literal.
4. is a little sneaky perhaps. The
unicode escape character is incorrect
QUESTION10
What is the result of trying to compile and run the following code.
public static void main(String[] args){
double d = 10 / 0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
1. output Positive infinity
2. output Negative infinity
3. Will fail to compile
4. Runtime exception
ANS : 4
Corr answer : 4
Division by zero on integer literals will throw
a runtime error.
Q. 1
Which colour is used to indicate instance methods
in the standard "javadoc" format documentation:
A.
blue
B.
red
C.
purple
D.
orange
Select the most appropriate answer.
Q. 2
What is the correct ordering for the import,
class and package declarations when found in a single file?
A.
package, import, class
B.
class, import, package
C.
import, package, class
D.
package, class, import
Select the most appropriate answer.
Q. 3
Which methods can be legally applied to a string
object?
A.
equals(String)
B.
equals(Object)
C.
trim()
D.
round()
E.
toString()
Select all correct answers.
Q. 4
What is the parameter specification for the
public static void main method?
A.
String args []
B.
String [] args
C.
Strings args []
D.
String args
Select all correct answers.
Q. 5
What does the zeroth element of the string array
passed to the public static void main method contain?
A.
The name of the program
B.
The number of arguments
C.
The first argument if one is present
Select the most appropriate answer.
Q. 6
Which of the following are Java keywords?
A.
goto
B.
malloc
C.
extends
D.
FALSE
Select all correct answers
Q. 7
What will be the result of compiling the
following code:
public class Test {
public static void main (String args []) {
int age;
age = age + 1;
System.out.println("The age is " + age);
}
}
A.
Compiles and runs with no output
B.
Compiles and runs printing out
The age is 1
C.
Compiles but generates a runtime error
D.
Does not compile
E.
Compiles but generates a compile time error
Select the most appropriate answer.
Q. 8
Which of these is the correct format to use to
create the literal char value a?
A.
‘a’
B.
"a"
C.
new Character(a)
D.
\000a
Select the most appropriate answer.
Q. 9
What is the legal range of a byte integral type?
A.
0 - 65, 535
B.
(–128) – 127
C.
(–32,768) – 32,767
D.
(–256) – 255
Select the most appropriate answer.
Q. 10
Which of the following is illegal:
A.
int i = 32;
B.
float f = 45.0;
C.
double d = 45.0;
Select the most appropriate answer.
Q. 11
What will be the result of compiling the
following code:
public class Test {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}
}
A.
Compiles and runs with no output
B.
Compiles and runs printing out
The age is 1
C.
Compiles but generates a runtime error
D.
Does not compile
E.
Compiles but generates a compile time error
Select the most appropriate answer.
Q. 12
Which of the following are correct?
A.
128 >> 1 gives 64
B.
128 >>> 1 gives 64
C.
128 >> 1 gives –64
D.
128 >>> 1 gives –64
Select all correct answers
Q. 13
Which of the following return true?
A.
"john" == "john"
B.
"john".equals("john")
C.
"john" = "john"
D.
"john".equals(new Button("john"))
Select all correct answers.
Q. 14
Which of the following do not lead to a runtime
error?
A.
"john" + " was " + " here"
B.
"john" + 3
C.
3 + 5
D.
5 + 5.5
Select all correct answers.
Q. 15
Which of the following are so called "short
circuit" logical operators?
A.
&
B.
||
C.
&&
D.
|
Select all correct answers.
Q. 16
Which of the following are acceptable?
A.
Object o = new Button("A");
B.
Boolean flag = true;
C.
Panel p = new Frame();
D.
Frame f = new Panel();
E.
Panel p = new Applet();
Select all correct answers.
Q. 17
What is the result of compiling and running the
following code:
public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}
}
A.
The class will not compile
B.
The compiler reports and error at line 2
C.
The compiler reports an error at line 9
D.
The value 10 is one of the elements printed to
the standard output
E.
The class compiles but generates a runtime error
Select all correct answers.
Q 18
Which of the following is correct:
A.
String temp [] = new String {"j" "a" "z"};
B.
String temp [] = { "j " " b" "c"};
C.
String temp = {"a", "b", "c"};
D.
String temp [] = {"a", "b", "c"};
Select the most appropriate answer.
Q. 19
What is the correct declaration of an abstract
method that is intended to be public:
A.
public abstract void add();
B.
public abstract void add() {}
C.
public abstract add();
D.
public virtual add();
Select the most appropriate answer.
Q. 20
Under what situations do you obtain a default
constructor?
A.
When you define any class
B.
When the class has no other constructors
C.
When you define at least one constructor
Select the most appropriate answer.
Q. 21
Given the following code:
public class Test {
…
}
Which of the following can be used to define a
constructor for this class:
A.
public void Test() {…}
B.
public Test() {…}
C.
public static Test() {…}
D.
public static void Test() {…}
Select the most appropriate answer.
Q. 22
Which of the following are acceptable to the Java
compiler:
A.
if (2 == 3) System.out.println("Hi");
B.
if (2 = 3) System.out.println("Hi");
C.
if (true) System.out.println("Hi");
D.
if (2 != 3) System.out.println("Hi");
E.
if (aString.equals("hello"))
System.out.println("Hi");
Select all correct answers.
Q. 23
Assuming a method contains code which may raise
an Exception (but not a RuntimeException), what is the correct
way for a method to indicate that it expects the caller to
handle that exception:
A.
throw Exception
B.
throws Exception
C.
new Exception
D.
Don't need to specify anything
Select the most appropriate answer.
Q. 24
What is the result of executing the following
code, using the parameters 4 and 0:
public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}
A.
Prints out: Exception Finally
B.
Prints out: Finally
C.
Prints out: Exception
D.
No output
Select the most appropriate answer.
Q.25
Which of the following is a legal return type of
a method overloading the following method:
public void add(int a) {…}
A.
void
B.
int
C.
Can be anything
Select the most appropriate answer.
Q.26
Which of the following statements is correct for
a method which is overriding the following method:
public void add(int a) {…}
A.
the overriding method must return void
B.
the overriding method must return int
C.
the overriding method can return whatever it
likes
Select the most appropriate answer.
Q. 27
Given the following classes defined in separate
files:
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("Car: drive");
}
}
public class Test {
public static void main (String args []) {
Vehicle v;
Car c;
v = new Vehicle();
c = new Car();
v.drive();
c.drive();
v = c;
v.drive();
}
}
What will be the effect of compiling and running
this class Test?
A.
Generates a Compiler error on the statement v= c;
B.
Generates runtime error on the statement v= c;
C.
Prints out:
Vehicle: drive
Car: drive
Car: drive
D.
Prints out:
Vehicle: drive
Car: drive
Vehicle: drive
Select the most appropriate answer.
Q. 28
Where in a constructor, can you place a call to a
constructor defined in the super class?
A.
Anywhere
B.
The first statement in the constructor
C.
The last statement in the constructor
D.
You can't call super in a constructor
Select the most appropriate answer.
Q. 29
Which variables can an inner class access from
the class which encapsulates it?
A.
All static variables
B.
All final variables
C.
All instance variables
D.
Only final instance variables
E.
Only final static variables
Select all correct answers.
Q. 30
What class must an inner class extend:
A.
The top level class
B.
The Object class
C.
Any class or interface
D.
It must extend an interface
Select the most appropriate answer.
-------------------> SCJP
Mock Exam - 1 (31 - 65 ) |