Java Programmer I

Multiple Choice Question

QID: 
523

Consider the following code:

class A {
public void doA(int k) throws Exception {        // 0
for(int i=0; i< 10; i++) {
if(i == k) throw new Exception("Index of k is "+i);        // 1
}
}
public void doB(boolean f) {       // 2
if(f) {
doA(15); // 3
}
else return;
}
public static void main(String[] args) { // 4
A a = new A();
a.doB(args.length>0); // 5
}
}

Which of the following statements are correct?

Select 1 option

A. This will compile and run without any errors or exception.
B. This will compile if throws Exception is added at line //2
C. This will compile if throws Exception is added at line //4
D. This will compile if throws Exception is added at line //2 as well as //4
E. This will compile if line marked // 1 is enclosed in a try - catch block.

Multiple Choice Question

QID: 
522

Following is a supposedly robust method to parse an input for a float :

public float parseFloat(String s){
float f = 0.0f;
try{
f = Float.valueOf(s).floatValue();
return f ;
}
catch(NumberFormatException nfe){
System.out.println("Invalid input " + s);
f = Float.NaN ;
return f;
}
finally { System.out.println("finally"); }
return f ;
}

Which of the following statements about the above method are true??

Select 1 option

A. If input is "0.1" then it will return 0.1 and print finally.
B. If input is "0x.1" then it will return Float.Nan and print Invalid Input 0x.1 and finally.
C. If input is "1" then it will return 1.0 and print finally.
D. If input is "0x1" then it will return 0.0 and print Invalid Input 0x1 and finally.
E. The code will not compile.

Multiple Choice Question

QID: 
521

Considering the following program, which of the options are true?

public class FinallyTest{
public static void main(String args[]){
try{
if (args.length == 0) return;
else throw new Exception("Some Exception");
}
catch(Exception e){
System.out.println("Exception in Main");
}  finally{
System.out.println("The end");
}
} }

Select 2 options

A. If run with no arguments, the program will only print 'The end'.
B. If run with one argument, the program will only print 'The end'.
C. If run with one argument, the program will print 'Exception in Main' and 'The end'.
D. If run with one argument, the program will only print 'Exception in Main'.
E. Only one of the above is correct.

Multiple Choice Question

QID: 
520

What will be the output of the following program:

public class TestClass{
public static void main(String args[]){
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");
}     // IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1(){
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}

Select 3 options

A. The program will print m1 Starts.
B. The program will print m1 Starts, 1 and 4, in that order.
C. The program will print m1 Starts, 1 and 2, in that order.
D. The program will print m1 Starts, 1, 2 and 4 in that order.
E. END will not be printed.

Multiple Choice Question

QID: 
519

What is wrong with the following code written in a single file named TestClass.java?

class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }
public class TestClass{
public static void main(String args[]) throws SomeThrowable{
try{
m1();
}catch(SomeThrowable e){
throw e;
}finally{
System.out.println("Done");
}
}
 public static void m1() throws MyThrowable{
throw new MyThrowable();
}  
 }

Select 2 options

A. The main declares that it throws SomeThrowable but throws MyThrowable.
B. You cannot have more than 2 classes in one file.
C. The catch block in the main method must declare that it catches MyThrowable rather than SomeThrowable.
D. There is nothing wrong with the code.
E. Done will be printed.

Pages

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)