latest articles

Recent Posts Widget

SCJP6 Mock Exam1 part 1

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.
Read more ...

Exceptions (QCM1 Answered)



01. Correct Options are : C D
Explanation
A. If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.
Any remaining code of the method will not be executed. Further, any uncaught exception will cause the JVM to kill the thread.
B. An overriding method must declare that it throws the same exception classes as the method it overrides.
It can throw any subset of the exceptions thrown by overridden class.
C. The main method of a program can declare that it throws checked exceptions. Any method can do that !
D. A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class. Note that it cannot throw the instances of any superclasses of the exception.
E. finally blocks are executed if and only if an exception gets thrown while inside the corresponding try block. Finally is ALWAYS executed. (Only exception is System.exit() )

Normal execution will not resume if an exception is uncaught by a method. The exception will propagate up the method invocation stack until some method handles it. If no one handles it then the exception will be handled by the JVM and the JVM will terminated that thread.
An overriding method only needs to declare that it can throw a subset of the exceptions the overridden method can throw. Having no throws clause in the overriding method is OK.
02. Correct Options are : A D

Non runtime exception must be declared in the throws clause.Back to Questions
03.Correct Options are : C D E

Explanation:
You can only throw a Throwable using a throws clause. Exception and Error are two
main subclasses of Throwable.
04.Correct Option is : A
Explanation:
m2() throws NewException, which is not caught anywhere. But before exiting out of
the main method, finally must be executed. Since finally throw AnotherException
(due to a call to m3() ), the NewException thrown in the try block ( due to call to
m2() ) is ignored and AnotherException is thrown from the main method.
05.Correct Option is : D

Explanation:
Syntax of try/catch/finally is:
try{
}c
atch(Exception1 e) {... }
catch(Exception2 e) {... }
...
catch(ExceptionN e) {... }
finally { ... }
With a try, either a catch and or finally or both can occur.
A try MUST be followed by at least one catch or finally. (Unless it is a try with resources statement, which is not in scope for this exam.)
In Java 7, you can collapse the catch blocks into a single one:
try {
...
}
catch (SQLException | IOException | RuntimeException e) {
//In this block, the class of the actual exception object will be whatever exception is thrown at //runtime.
//But the class of the reference e will be the closest common super class of all the exceptions in //the catch block.
//In this case, it will be java.lang.Exception because that is the most specific class that is a super //class for all the three exceptions.
e.printStackTrace();
}
06.Correct Options are : A E

B. InputException
There is an java.io.IOException but no InputException or OutputException.
D. MemoryException
There is an OutOfMemoryError but no MemoryException. There is also a StackOverflowError.
E. SecurityException
Java has a java.lang.SecurityException. This exception extends RuntimeException and is thrown by the security manager upon security violation. For example, when a java program runs in a sandbox (such as an applet) and it tries to use prohibited APIs such as File I/O, the security manager
throws this exception.

Back to Questions
07.Correct Option is : A
 Explanation:
A. Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 10
at exceptions.TestClass.doTest(TestClass.java:24)
at exceptions.TestClass.main(TestClass.java:14)
You are creating an array of length 10. Since array numbering starts with 0, the last element would be array[9].
array[10] would be outside the range of the array and therefore an ArrayIndexOutOfBoundsException will be thrown, which cannot be caught by catch(MyException ) clause.
The exception is thus thrown out of the main method and is handled by the JVM's uncaught exception handling mechanism, which prints the stack trace.
B. Error in thread "main" java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException extends java.lang.RuntimeException, which in turn extends java.lang.Exception. Therefore, ArrayIndexOutOfBoundsException is an Exception and not an Error.
C. exceptions.MyException: Exception from doAnotherTest
D. exceptions.MyException: Exception from doAnotherTest
at exceptions.TestClass.doAnotherTest(TestClass.java:29)
at exceptions.TestClass.doTest(TestClass.java:25)
at exceptions.TestClass.main(TestClass.java:14)

Note that there are questions in the exam that test your knowledge about how exception
messages are printed.
When you use System.out.println(exception), a stack trace is not printed. Just the name of the exception class and the message is printed.
When you use exception.printStackTrace(), a complete chain of the names of the methods called, along with the line numbers, is printed from the point where the exception was thrown and up to the point where the exception was caught.

08.Correct Option is : C
A. It will not compile.
It will compile fine.
B. It will not print anything and will throw NullPointerException
C. It will print calculating and then throw NullPointerException.
After printing, when it tries to call calculate() on x, it will throw
NullPointerException since x is null.
D. It will print calculating and will throw NoSuchMethodError
E. It will print calculating and will throw MethodNotImplementedException
09.Correct Option is : D

Explanation:
Observe that all the exceptions given in the options other than Exception and NoClassDefFoundError are RuntimeExceptions. These are usually thrown implicitly. A programmer should not throw these exceptions explicitly.
java.lang.Exception and its subclasses (except RuntimeException) should be used by the programmer to reflect known exceptional situations, while RuntimeExceptions are used to reflect unforseen or unrecoverable exceptional situations.
Note: There is no hard and fast rule that says RuntimeExceptions (such as the ones mentioned in this questions) must not be thrown explicitly. It is ok to throw these exceptions explicitly in certain situations. For example, framework/library classes such as Struts, Spring, and Hibernate, and standard JDK classes throw these exceptions explicitly. But for the purpose of the exam, it is a good way to determine if a given application should be thrown expliclity by the programmer or not.
10.Correct Option is : E

Explanation:
When you pass 6 to luckyNumber(), if(seed%2 == 0) throw new Exception("No Even no."); is executed and the exception is caught by the catch block where it tries to return 3; But as there is a finally associated with the try/catch block, it is executed before anything is returned. Now, as finally has return 7;, this value supersedes 3.
In fact, this method will always return 7 if seed <= 10.
Now, in the switch there is no break statement. So both -
case 7: amount = amount * 2;
and case 6: amount = amount + amount;
are executed. so the final amount becomes 400.
Read more ...

Exceptions QCM1 questions



01.Which of these statements are true?
Select 2 options
A. If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.
B. An overriding method must declare that it throws the same exception classes as the method it overrides.
C. The main method of a program can declare that it throws checked exceptions.
D. A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.
E. finally blocks are executed if and only if an exception gets thrown while inside the corresponding try block.
02.Assume that a method named 'method1' contains code which may raise a non-runtime (checked) Exception.
What is the correct way to declare that method so that it indicates that it expects the caller to handle that exception?
Select 2 options
A. public void method1() throws Throwable
B. public void method1() throw Exception
C. public void method1() throw new Exception
D. public void method1() throws Exception
E. public void method1()
03.Which of the following can be thrown using a throw statement?
Select 3 options
A. Event
B. Object
C. Throwable
D. Exception
E. RuntimeException
04.What will be the result of compiling and running the following program ?
class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
public static void main(String [] args) throws Exception{
try{
m2();
}
finally{ m3(); }
}
public static void m2() throws NewException{ throw new NewException();
public static void m3() throws AnotherException{ throw new AnotherException(); }
Select 1 option
A. It will compile but will throw AnotherException when run.
B. It will compile but will throw NewException when run.
C. It will compile and run without throwing any exceptions.
D. It will not compile.
E. None of the above.
05.What will be the result of compiling and running the following program ?
class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
public static void main(String[] args) throws Exception{
try{
     m2();
}
finally{
     m3();
}
catch (NewException e){}
}
public static void m2() throws NewException {
throw new NewException();
public static void m3() throws AnotherException{ throw new AnotherException(); }
}

Select 1 option
A. It will compile but will throw AnotherException when run.
B. It will compile but will throw NewException when run.
C. It will compile and run without throwing any exceptions.
D. It will not compile.
E. None of the above.
06.Which of the following are standard Java exception classes?
Select 2 options
A. FileNotFoundException
B. InputException
C. CPUError
D. MemoryException
E. SecurityException
07.What will be the output when the following program is run?
package exceptions;
public class TestClass {
public static void main(String[] args) {
try{
doTest();
}
catch(MyException me){
System.out.println(me);
}
}
static void doTest() throws MyException{
int[] array = new int[10];
array[10] = 1000;
doAnotherTest();
}
static void doAnotherTest() throws MyException{
throw new MyException("Exception from doAnotherTest");
}
}
class MyException extends Exception {
public MyException(String msg){
super(msg);
}
}
Select 1 option
A. Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 10
at exceptions.TestClass.doTest(TestClass.java:24)
at exceptions.TestClass.main(TestClass.java:14)
B. Error in thread "main" java.lang.ArrayIndexOutOfBoundsException
C. exceptions.MyException: Exception from doAnotherTest
D. exceptions.MyException: Exception from doAnotherTest
at exceptions.TestClass.doAnotherTest(TestClass.java:29)
at exceptions.TestClass.doTest(TestClass.java:25)
at exceptions.TestClass.main(TestClass.java:14)
08.What will the following code print when compiled and run?
abstract class Calculator{
abstract void calculate();
public static void main(String[] args){
System.out.println("calculating");
Calculator x = null;
x.calculate();
}
}
Select 1 option
A. It will not compile.
B. It will not print anything and will throw NullPointerException
C. It will print calculating and then throw NullPointerException.
D. It will print calculating and will throw NoSuchMethodError
E. It will print calculating and will throw MethodNotImplementedException
09.A Java programmer is developing a desktop application. Which of the following
exceptions would be appropriate for him to throw explicitly from his code?
Select 1 option
A. NullPointerException
B. ClassCastException
C. ArrayIndexOutofBoundsException
D. Exception
E. NoClassDefFoundError
Check Answer
10.What will the following code print?
public class Test{
public int luckyNumber(int seed){
if(seed > 10) return seed%10;
int x = 0;
try{
if(seed%2 == 0) throw new Exception("No Even no.");
else return x;
}
catch(Exception e){
return 3;
}
finally{
return 7;
}
}
public static void main(String args[]){
int amount = 100, seed = 6;
switch( new Test().luckyNumber(6) ){
case 3: amount = amount * 2;
case 7: amount = amount * 2;
case 6: amount = amount + amount;
default :
}
System.out.println(amount);
}
}
Select 1 option
A. It will not compile.
B. It will throw an exception at runtime.
C. 800
D. 200
E. 400
Read more ...