Quiz
02
Crie uma conta grátis para terminar este quiz
Salve seu progresso e veja sua nota final.
Já tem uma conta? Inicie sessão
Perguntas do quiz (80)
1Which line, ifany, will give a compile time error ?
void test( byte x) {
switch( x) {
case ' a' : // 1
case 256: // 2
case 0: // 3
default : // 4
case 80: // 5
}
}- ALine 1 as 'a' is not compatible with byte
- BLine 2 as 256 cannot fit into a byte.
- CNo compile time error but a run time error at line 2.
- DLine 4 as the default label must be the last label in the switch statement.
- EThere is nothing wrong with the code.
2What is wrong with the following code?
class MyException extends Exception { }
public class TestClass{
public static void main( String[ ] args) {
TestClass tc = new TestClass( ) ;
try{
tc. m1( ) ;
}c
atch ( MyException e) {
tc. m1( ) ;
}f
inally{
tc. m2( ) ;
}
}p
ublic void m1( ) throws MyException{
throw new MyException( ) ;
}p
ublic void m2( ) throws RuntimeException{
throw new NullPointerException( ) ;
}
}- AIt will not compile because you cannot throw an exception in finally block
- BIt will not compile because you cannot throw an exception in catch block.
- CIt will not compile because NullPointerException cannot be created this way
- DIt will not compile because ofunhandled exception
- EIt will compile but will throw an exception when run
3What will be the result ofattempting to compile and run the following code?
class TestClass{
public static void main( String args[ ] ) {
String str1 = " str1" ;
String str2 = " str2" ;
System. out. println( str1. concat( str2) ) ;
System. out. println( str1) ;
}
}- AThe code will fail to compile.
- BThe program will print str1 and str1.
- CThe program will print str1 and str1str2
- DThe program will print str1str2 and str1
- EThe program will print str1str2 and str1str2.
4What is the result ofexecuting the following fragment ofcode:
boolean b1 = false;
boolean b2 = false;
if ( b2 ! = b1 = ! b2) {
System. out. println( " true" ) ;
}e
lse{
System. out. println( " false" ) ;
}- ACompile time error
- BIt will print true.
- CIt will print false
- DRuntime error
- EIt will print nothing
5Which ofthe following are benefits ofArrayList over an array?
- AYou do not have to worry about the size ofthe ArrayList while inserting elements.
- BIt consumes less memory space.
- CYou do not have to worry about thread safety.
- DIt allows you to write type safe code.
6Given the following code:
class M { }
class N{
private M m = new M( ) ;
public void makeItNull( M pM) {
pM = null;
}p
ublic void makeThisNull( ) {
makeItNull( m) ;
}p
ublic static void main( String[ ] args) {
N n = new N( ) ;
n. makeThisNull( ) ;
}
}Whi
ch ofthe following statements are correct?- AThere are no instances ofM to be garbage collected till the program ends.
- BA call to n. makeThisNull( ) marks the private instance ofM for garbage
collection. - CSetting pM = null; in makeItNull( ) , marks the private instance ofM for garbage
collection. - Dprivate members ofa class become eligible for garbage collection only when the
instance ofthe class itselfbecomes eligible for garbage collection.
7package p;
private class TC extends j ava. util. HashMap{
public TC( ) {
super( 100) ;
System. out. println( " TC created" ) ;
}
}p
ublic class TestClass extends TC{
public TestClass( ) {
System. out. println( " TestClass created" ) ;
}p
ublic static void main( String[ ] args) { new TestClass( ) ; }
}Wha
t will be the output when TestClass is run?- A" TestClass created" as well as " TC created".
- BIt will not compile because HashMap is a final class.
- COnly " TestClass created" will be printed.
- DOnly " TC created" will be printed.
- ENone ofthe above are correct.
8class TestClass{
public static void main( String args[ ] ) {
int k = 0;
int m = 0;
for ( int i = 0; i <= 3; i++) {
k++;
if ( i == 2) {
// line 1
}m
++;
}S
ystem. out. println( k + " , " + m ) ;
}
}- AIt will print 3, 2 when line 1 is replaced by break;
- BIt will print 3, 2 when line 1 is replaced by continue.
- CIt will print 4, 3 when line 1 is replaced by continue.
- DIt will print 4, 4 when line 1 is replaced by i = m++;
- EIt will print 3, 3 when line 1 is replaced by i = 4;
9What can be the type ofa catch argument ?
- AAny class that extends j ava. lang. Exception
- BAny class that extends j ava. lang. Exception except any class that extends
j ava. lang. RuntimeException - CAny class that is-a Throwable.
- DAny Object
- EAny class that extends Error
10public class Varargs{
public void test( ) {
test1( 10) ; //1
test1( 10, 20) ; //2
}p
ublic static void main( String[ ] args) {
new Varargs( ) . test( ) ;
}/
/insert method here.
}Whi
ch ofthe following lines can be added independently to the above class so that it
will run without any errors or exceptions?- Apublic void test1( int i, int j ) { }
- Bpublic void test1( int i, int. . . j ) { }
- Cpublic void test1( int. . . i) { }
- Dpublic void test1( int i. . . ) { }
- Epublic void test1( int[ ] i) { }
11What will be the result ofattempting to compile and run the following class?
public class IfTest{
public static void main( String args[ ] ) {
if ( true)
if ( false)
System. out. println( " True False" ) ;
else
System. out. println( " True True" ) ;
}
}- AThe code will fail to compile because the syntax ofthe if statement is not correct.
- BThe code will fail to compile because the values in the condition bracket are
invalid - CThe code will compile correctly and will not display anything.
- DThe code will compile correctly and will display True True.
12public class Conversion{
public static void main( String[ ] args) {
int i = 1234567890;
float f = i;
System. out. println( i - ( int) f) ;
}
}Wha
t will it print when run?- AIt will print 0.
- BIt will not print 0.
- CIt will not compile.
- DIt will throw an exception at runtime.
- ENone ofthe above
13The following program will print
j ava. lang. ArithmeticException: / by zero
class Test{
public static void main( String[ ] args) {
int d = 0;
try{
int i = 1 / ( d* doIt( ) ) ;
} catch ( Exception e) {
System. out. println( e) ;
}
}p
ublic static int doIt( ) throws Exception{
throw new Exception( " Forget It" ) ;
}
}- ATrue
- BFalse
14The following class will print 'index = 2' when compiled and run.
class Test{
public static int[ ] getArray( ) { return null; }
public static void main( String[ ] args) {
int index = 1;
try{
getArray( ) [ index=2] ++;
}c
atch ( Exception e) { } //empty catch
System. out. println( " index = " + index) ;
}
}- ATrue
- BFalse
15An abstract method cannot be overridden
- ATrue
- BFalse
16What will be the output ofthe following code snippet?
int a = 1;
int[ ] ia = new int[ 10] ;
int b = ia[ a] ;
int c = b + a;
System. out. println( b = c) ;- A0
- B1
- C2
- Dtrue
- Efalse
17Which ofthe following is illegal ?
- Achar c = 320;
- Bfloat f= 320;
- Cdouble d = 320;
- Dbyte b = 320;
- ENone ofthe above is illegal.
18You want to invoke the overridden method (the method in the base class) from the
overriding method (the method in the derived class) named m( ) .
Which ofthe following constructs which will let you do that?- Asuper. m( ) ;
- Bsuper. this( ) ;
- Cstatic { INTERVAL = 10; }
- Dparent. m( ) ;
- Esuper( ) ;
19Given the following class, which ofthe given blocks can be inserted at line 1 without
errors?
public class InitClass{
private static int loop = 15 ;
static final int INTERVAL = 10 ;
boolean flag ;
//line 1
}- Astatic { System. out. println( " Static" ) ; }
- Bstatic { loop = 1; }
- Cstatic { loop += INTERVAL; }
- Dstatic { INTERVAL = 10; }
- E{ flag = true; loop = 0; }
20Which ofthe following statements are correct?
- AAn abstract class can be extended by an abstract or a concrete class.
- BA concrete class can be extended by an abstract or a concrete class.
- CAn interface can be extended by another interface.
- DAn interface can be extended by an abstract class.
- EAn interface can be extended by a concrete class.
- FAn abstract class cannot implement an interface.
21Which statements concerning the following code are true?
class A{
public A( ) { } // A1
public A( String s) { this( ) ; System. out. println( " A : " +s) ; } // A2
}c
lass B extends A{
public int B( String s) { System. out. println( " B : " +s) ; return 0; } // B1
}c
lass C extends B{
private C( ) { super( ) ; } // C1
public C( String s) { this( ) ; System. out. println( " C : " +s) ; } // C2
public C( int i) { } // C3
}- AAt least one ofthe constructors ofeach class is called as a result ofconstructing an
object ofclass C. - BConstructor at //A2 will never be called in creation ofan object ofclass C
- CClass C can be instantiated only in two ways by users ofthis class.
- D//B1 will never be called in creation ofobjects ifclass A, B, or C
- EThe code will not compile.
- AAt least one ofthe constructors ofeach class is called as a result ofconstructing an
22Consider the following class and interface definitions (in separate files):
public class Sample implements IInt{
public static void main( String[ ] args) {
Sample s = new Sample( ) ; //1
int j = s. thevalue; //2
int k = IInt. thevalue; //3
int l = thevalue; //4
}
}p
ublic interface IInt{
int thevalue = 0;
}Wha
t will happen when the above code is compiled and run?- AIt will give an error at compile time at line //1.
- BIt will give an error at compile time at line //3.
- CIt will give an error at compile time at line //2.
- DIt will give an error at compile time at line //4.
- EIt will compile and run without any problem.
23Consider the following lines ofcode:
Integer i = new Integer( 42) ;
Long ln = new Long( 42) ;
Double d = new Double( 42. 0) ;
Which ofthe following options are valid?- Ai == ln;
- Bln == d;
- Ci. equals( d) ;
- Dd. equals( ln) ;
- Eln. equals( 42) ;
24Which line(s) in the following code will cause a compilation error?
static import j ava. lang. System. *; //1
class $$ //2
{
static public void main( String. . . _$_) //3
{
String _ = " " ; //4
for( int $=0; ++$ < _$_. length; ) //5
_
+= _$_[ $] ; //6
out. println( _) ; //7
}
}- A1
- B2
- C3
- D4
- E5
- F6
- G7
- HNone ofthe lines is invalid.
25Which ofthe following is not a primitive data value in Java?
- A" x"
- B' x'
- C10. 2F
- DObj ect
- Efalse
26public class BreakTest{
public static void main( String[ ] args) {
int i = 0, j = 5;
lab1 : for( ; ; i++) {
for( ; ; --j ) if( i >j ) break lab1;
}S
ystem. out. println( " i = " +i+" , j = " +j ) ;
}
}- Ai = 1, j = -1
- Bi = 1, j = 4
- Ci = 0, j = 4
- Di = 0, j = -1
- EIt will not compile.
27Consider that you are writing a set ofclasses related to a new Data Transmission
Protocol and have created your own exception hierarchy derived from
java.lang.Exception as follows:
enthu. trans. ChannelException
+-- enthu. trans. DataFloodingException,
enthu. trans. FrameCollisionException
You have a TransSocket class that has the following method:
long connect( String ipAddr) throws ChannelException
Now, you also want to write another "AdvancedTransSocket" class, derived from
"TransSocket" which overrides the above mentioned method. Which ofthe following
are valid declaration ofthe overriding method?- Aint connect( String ipAddr) throws DataFloodingException
- Bint connect( String ipAddr) throws ChannelException
- Clong connect( String ipAddr) throws FrameCollisionException
- Dlong connect( String ipAddr) throws Exception
- Elong connect( String str)
28Compared to public, protected and private accessibility, default accessibility is....
- ALess restrictive than public
- BMore restrictive than public, but less restrictive than protected.
- CMore restrictive than protected, but less restrictive than private.
- DMore restrictive than private.
- ELess restrictive than protected from within a package, and more restrictive than
protected from outside a package.
29Which ofthe changes given in options can be done (independent ofeach other) to let
the following code compile and run without errors?
class SomeClass{
String s1 = " green mile" ; // 0
public void generateReport( int n ) {
String local; // 1
if( n > 0 ) local = " good" ; //2
System. out. println( s1+" = " + local ) ; //3
}
}- AInsert after line 2 : else local = " bad" ;
- BInsert after line 2 : if( n <= 0) local = " bad" ;
- CMove line 1 and place it after line 0.
- Dchange line 1 to : final String local = " rocky" ;
- EThe program already is without any errors.
30Which ofthe following correctly defines a method named stringProcessor that can be
called by other programmers as follows: stringProcessor( str1) or
stringProcessor( str1, str2) or stringProcessor( str1, str2, str3) ,
where str1, str2, and str3 are references to Strings.- Apublic void stringProcessor( . . . String) {
} - Bpublic void stringProcessor( String. . . strs) {
} - Cpublic void stringProcessor( String[ ] strs) {
- Dpublic void stringProcessor( String a, String b, String c) {
- EThree separate methods need to be written.
- Apublic void stringProcessor( . . . String) {
31Assume that a, b, and c refer to instances ofprimitive wrapper classes. Which ofthe
following statements are correct?
Note: Although primitive wrapper classes are not explicitly mentioned in the exam
objectives, we have seen some candidates get questions on this aspect ofwrapper
classes.- Aa.equals(a) will always return true.
- Bb.equals(c) may return false even ifc.equals(b) returns true.
- Ca.equals(b) returns same as a == b.
- Da.equals(b) throws an exception ifthey refer to instances ofdifferent classes.
- Ea.equals(b) returns false ifthey refer to instances ofdifferent classes.
32Which ofthe following are true about the "default" constructor?
- AIt is provided by the compiler only ifthe class and any ofits super classes does not
define any constructor. - BIt takes no arguments.
- CA default constructor is used to return a default value.
- DTo define a default constructor, you must use the default keyword.
- EIt is always public.
- AIt is provided by the compiler only ifthe class and any ofits super classes does not
33Consider the following program :
class Test{
public static void main( String[ ] args) {
short s = 10; // 1
char c = s; // 2
s = c; // 3
}
}I
dentify the correct statements.- ALine 3 is not valid.
- BLine 2 is not valid.
- CIt will compile because both short and char can hold 10.
- DNone ofthe lines 1, 2 and 3 is valid.
34Consider the following class hierarchy shown in the image. (B1 and B2 are subclasses
ofA and C1, C2 are subclasses ofB1)
Assume that method public void m1( ) { . . . } is defined in all ofthese classes
EXCEPT B1 and C1.
Assume that " obj ectOfXX" means a variable that points to an object ofclass XX. So,
obj ectOfC1 means a reference variable that is pointing to an object ofclass C1.
Which ofthe following statements are correct?- Aobj ectOfC1. m1( ) ; will cause a compilation error.
- Bobj ectOfC2. m1( ) ; will cause A's m1( ) to be called.
- Cobj ectOfC1. m1( ) ; will cause A's m1( ) to be called.
- Dobj ectOfB1. m1( ) ; will cause an exception at runtime.
- Eobj ectOfB2. m1( ) ; will cause an exception at runtime.
35Which ofthe following method calls can be applied to a String object?
- Aequals( Obj ect)
- BequalsIgnoreCase( String)
- Cprune( )
- Dappend( )
- Eintern( )
36The following code snippet will print 'true'.
short s = Short. MAX_VALUE;
char c = s;
System. out. println( c == Short. MAX_VALUE) ;- ATrue
- BFalse
37What letters will be printed by this program?
public class ForSwitch{
public static void main( String args[ ] ) {
char i;
LOOP: for ( i=0; i<5; i++) {
switch( i++) {
case ' 0' : System. out. println( " A" ) ;
case 1: System. out. println( " B" ) ; break LOOP;
case 2: System. out. println( " C" ) ; break;
case 3: System. out. println( " D" ) ; break;
case 4: System. out. println( " E" ) ;
case ' E' : System. out. println( " F" ) ;
}
}
}
}- AA
- BB
- CC
- DD
- EF
38What will the following program print when run using the command line: j ava
TestClass
public class TestClass {
public static void methodX( ) throws Exception {
throw new AssertionError( ) ;
}p
ublic static void main( String[ ] args) {
try{
methodX( ) ;
}c
atch( Exception e) {
System. out. println( " EXCEPTION" ) ;
}
}
}- AIt will throw AssertionError out ofthe main method.
- BIt will print EXCEPTION.
- CIt will not compile because ofthe throws clause in methodX( ) .
- DIt will end without printing anything because assertions are disabled by default.
39Which ofthe following methods modify the object on which they are called?
- AsetValue( int) ofjava.lang.Integer class.
- BThe substring( int) method ofthe String class
- CThe replace( ) method ofthe String class.
- DThe reverse( ) method ofthe StringBuffer class.
- ENone ofthese.
40Consider the following code snippet:
XXXX m ;
switch( m ) {
case 32 : System. out. println( " 32" ) ; break;
case 64 : System. out. println( " 64" ) ; break;
case 128 : System. out. println( " 128" ) ; break;
}
What type can 'm' be ofso that the above code compiles and runs as expected ?- Aint m;
- Blong m;
- Cchar m;
- Dbyte m;
- Eshort m;
41In the following code, after which statement (earliest), the object originally held in s,
may be garbage collected ?
1. public class TestClass{
2. public static void main ( String args[ ] ) {
3. Student s = new Student( " Vaishali" , " 930012" ) ;
4. s. grade( ) ;
5. System. out. println( s. getName( ) ) ;
6. s = null;
7. s = new Student( " Vaishali" , " 930012" ) ;
8. s. grade( ) ;
9. System. out. println( s. getName( ) ) ;
10 s = null;
}
}
public class Student{
private String name, rollNumber;
public Student( String name, String rollNumber) {
this. name = name;
this. rollNumber = rollNumber;
}/
/valid setter and getter for name and rollNumber follow
public void grade( ) {
}
}- AIt will not be Garbage Collected till the end ofthe program.
- BLine 5
- CLine 6
- DLine 7
- ELine 10
42What will the following code print?
public class Test{
public static void stringTest( String s) {
s. replace( ' h' , ' s' ) ;
}p
ublic static void stringBuilderTest( StringBuilder s) {
s. append( " o" ) ;
}p
ublic static void main( String[ ] args) {
String s = " hell" ;
StringBuilder sb = new StringBuilder( " well" ) ;
stringTest( s) ;
stringBuilderTest( sb) ;
System. out. println( s + sb) ;
}
}- Asellwello
- Bhellwello
- Chellwell
- Dsellwell
- ENone ofthese.
43What will the following program print when run?
public class ChangeTest {
private int myValue = 0;
public void showOne( int myValue) {
myValue = myValue;
}p
ublic void showTwo( int myValue) {
this. myValue = myValue;
}p
ublic static void main( String[ ] args) {
ChangeTest ct = new ChangeTest( ) ;
ct. showTwo( 200) ;
System. out. println( ct. myValue) ;
ct. showOne( 100) ;
System. out. println( ct. myValue) ;
}
}- A0 followed by 100.
- B100 followed by 100.
- C0 followed by 200.
- D100 followed by 200.
- E200 followed by 200.
- F200 followed by 100.
44Which statement regarding the following code is correct?
class A{
public int i = 10;
private int j = 20;
}c
lass B extends A{
private int i = 30; //1
public int k = 40;
}c
lass C extends B{
}p
ublic class TestClass{
public static void main( String args[ ] ) {
C c = new C( ) ;
System. out. println( c. i) ; //2
System. out. println( c. j ) ; //3
System. out. println( c. k) ;
}
}- AIt will print 10 and 40 if//3 is commented.
- BIt will print 40 if//2 and //3 are commented.
- CIt will not compile because of//1.
- DIt will compile if//2 is commented.
- ENone ofthese.
45Consider the following class written by a novice programmer.
class Elliptical{
public int radiusA, radiusB;
public int sum = 100;
public void setRadius( int r) {
if( r>99) throw new IllegalArgumentException( ) ;
radiusA = r;
radiusB = sum - radiusA;
}
}A
fter some time, the requirements changed and the programmer now wants to make
sure that radiusB is always (200 - radiusA) instead of(100 - radiusA) without
breaking existing code that other people have written. Which ofthe following will
accomplish his goal?- AMake sum = 200;
- BMake sum = 200 and make it private.
- CMake sum = 200 and make all fields (radiusA, radiusB, and sum) private.
- DWrite another method setRadius2( int r) and set radiusB accordingly in this
method. - EHis goal cannot be accomplished.
- FThis class will not compile.
46What will happen when the following program is compiled and run?
public class SM{
public String checkIt( String s) {
if( s. length( ) == 0 | | s == null) {
return " EMPTY" ;
}e
lse return " NOT EMPTY" ;
}p
ublic static void main( String[ ] args) {
SM a = new SM( ) ;
a. checkIt( null) ;
}
}- AIt will print EMPTY.
- BIt will print NOT EMPTY.
- CIt will throw NullPointerException.
- DIt will print EMPTY if|| is replaced with |.
47Consider the following code:
class Super { static String ID = " QBANK" ; }
class Sub extends Super{
static { System. out. print( " In Sub" ) ; }
}p
ublic class Test{
public static void main( String[ ] args) {
System. out. println( Sub. ID) ;
}
}- AIt will print In Sub and QBANK.
- BIt will print QBANK.
- CDepends on the implementation ofJVM.
- DIt will not even compile.
- ENone ofthe above.
48Consider the following method which is called with an argument of7:
public void method1( int i) {
int j = ( i*30 - 2) /100;
POINT1 : for( ; j <10; j ++) {
boolean flag = false;
while( ! flag) {
if( Math. random( ) >0. 5) break POINT1;
}
}
while( j >0) {
System. out. println( j --) ;
if( j == 4) break POINT1;
}
}Wha
t will it print?- AIt will print 1 and 2
- BIt will print 1 to N where N is a random number.
- CIt will not compile.
- DIt will throw an exception at runtime.
49Consider the following code:
interface Flyer{ }
class Bird implements Flyer { }
class Eagle extends Bird { }
class Bat { }
public class TestClass {
public static void main( String[ ] args) {
Flyer f = new Eagle( ) ;
Eagle e = new Eagle( ) ;
Bat b = new Bat( ) ;
if( f instanceof Flyer) System. out. println( " f is a Flyer" ) ;
if( e instanceof Bird) System. out. println( " e is a Bird" ) ;
if( b instanceof Bird) System. out. println( " f is a Bird" ) ;
}
}Wha
t will be printed when the above code is compiled and run?- AIt will not compile.
- BIt will throw an exception when run.
- Cfis a Flyer
e is a Bird - Dfis a Flyer
- Ee is a Bird
50What will be result ofattempting to compile this class?
import j ava. util. *;
package test;
public class TestClass{
public OtherClass oc = new OtherClass( ) ;
}c
lass OtherClass{
int value;
}- AThe class will fail to compile, since the class OtherClass is used before it is
defined. - BThere is no problem with the code.
- CThe class will fail to compile, since the class OtherClass must be defined in a file
called OtherClass.java - DThe class will fail to compile .
- ENone ofthe above.
- AThe class will fail to compile, since the class OtherClass is used before it is
51Given the following class definitions :
interface MyIface{ } ;
class A { } ;
class B extends A implements MyIface{ } ;
class C implements MyIface{ } ;
and the following object instantiations:
A a = new A( ) ;
B b = new B( ) ;
C c = new C( ) ;
Which ofthe following assignments are legal at compile time?- Ab = c;
- Bc = b;
- CMyIface i = c;
- Dc = ( C) b;
- Eb = a;
52Identify the correct constructs.
- AA.
try {
for( ; ; ) ;
} finally { } - Btry {
File f = new File( " c: \a. txt" ) ;
} catch { f = null; } - Cint k = 0;
try {
k = callValidMethod( ) ;
}S
ystem. out. println( k) ;
catch { k = -1; } - Dtry {
try {
Socket s = new ServerSocket( 3030) ;
} catch( Exception e) {
s = new ServerSocket( 4040) ;
}
} - Etry {
s = new ServerSocket( 3030) ;
}c
atch( Exception t) { t. printStackTrace( ) ; }
catch( IOException e) {
s = new ServerSocket( 4040) ;
}c
atch( Throwable t) { t. printStackTrace( ) ; } - Fint x = validMethod( ) ;
try {
if( x == 5) throw new IOException( ) ;
else if( x == 6) throw new Exception( ) ;
} finally {
x = 8;
}
catch( Exception e) { x = 9; }
- AA.
53Consider the following method...
public int setVar( int a, int b, float c) { . . . }
Which ofthe following methods correctly overload the above method?- Apublic int setVar( int a, float b, int c) {
return ( int) ( a + b + c) ;
} - Bpublic int setVar( int a, float b, int c) {
return this( a, c, b) ;
} - Cpublic int setVar( int x, int y, float z) {
return x+y;
} - Dpublic float setVar( int a, int b, float c) {
return c*a;
} - Epublic float setVar( int a) {
return a;
}
- Apublic int setVar( int a, float b, int c) {
54Consider the following code snippet:
void m1( ) throws Exception{
try{
// line1
}c
atch ( IOException e) {
throw new SQLException( ) ;
}c
atch( SQLException e) {
throw new InstantiationException( ) ;
}f
inally{
throw new CloneNotSupportedException( ) ; // this is not a RuntimeException.
}
}Whi
ch ofthe following statements are true?- AIfIOException gets thrown at line1, then the whole method will end up throwing
SQLException. - BIfIOException gets thrown at line1, then the whole method will end up throwing
CloneNotSupportedException. - CIfIOException gets thrown at line1, then the whole method will end up throwing
InstantiationException() - DIfno exception is thrown at line1, then the whole method will end up throwing
- EIfSQLException gets thrown at line1, then the whole method will end up throwing
InstantiationException()
- AIfIOException gets thrown at line1, then the whole method will end up throwing
55Which ofthe following methods can be called on a String object?
- Asubstring( int i)
- Bsubstring( int i, int j )
- Csubstring( int i, int j , int k)
- Dequals( Obj ect o)
56Which ofthe following operators can be used in conjunction with a String object?
- A+
- B++
- C+=
- D.
- E*
57The following code snippet will not compile:
int i = 10;
System. out. println( i<20 ? out1( ) : out2( ) ) ;
Assume that out1 and out2 have method signature: public void out1(); and public void
out2();- ATrue
- BFalse
58Consider the code shown below:
public class TestClass{
public static int switchTest( int k) {
int j = 1;
switch( k) {
case 1: j ++;
case 2: j ++;
case 3: j ++;
case 4: j ++;
case 5: j ++;
default : j ++;
}r
eturn j + k;
}p
ublic static void main( String[ ] args) {
System. out. println( switchTest( 4) ) ;
}
}Wha
t will it print when compiled and run?- A5
- B6
- C7
- D8
- E9
59What will the following program print?
class Test{
public static void main( String[ ] args) {
int i = 4;
int ia[ ] [ ] [ ] = new int[ i] [ i = 3] [ i] ;
System. out. println( ia. length + " , " + ia[ 0] . length+" , " + ia[ 0] [ 0] . length) ;
}
}- AIt will not compile.
- B3, 4, 3
- C3, 3, 3
- D4, 3, 4
- E4, 3, 3
60Consider the following hierarchy ofException classes :
j ava. lang. RuntimeException
+-------- IndexOutOfBoundsException
+---------ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException
Which ofthe following statements are correct for a method that can throw
ArrayIndexOutOfBounds as well as StringIndexOutOfBounds Exceptions but does
not have try catch blocks to catch the same?- AThe method calling this method will either have to catch these 2 exceptions or
declare them in its throws clause - BIt is ok ifit declares just throws ArrayIndexOutOfBoundsException
- CIt must declare throws ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException - DIt is ok ifit declares just throws IndexOutOfBoundsException
- EIt does not need to declare any throws clause.
- AThe method calling this method will either have to catch these 2 exceptions or
61What will the following code print ?
class Test{
public static void main( String[ ] args) {
int k = 1;
int[ ] a = { 1 } ;
k += ( k = 4) * ( k + 2) ;
a[ 0] += ( a[ 0] = 4) * ( a[ 0] + 2) ;
System. out. println( k + " , " + a[ 0] ) ;
}
}- AIt will not compile.
- B4 , 4
- C25 , 25
- D13 , 13
- ENone ofthe above.
62Given the following line ofcode:
List students = new ArrayList( ) ;
Identify the correct statement:- AThe reference type is List and the object type is ArrayList.
- BThe reference type is ArrayList and the object type is ArrayList.
- CThe reference type is ArrayList and the object type is List.
- DThe reference type is List and the object type is List.
63Consider the following code:
class Test{
public static void main( String[ ] args) {
for ( int i = 0; i < args. length; i++) System. out. print( i == 0
? args[ i] : " " + args[ i] ) ;
}
}Wha
t will be the output when it is run using the following command:
java Test good bye friend!- Agood bye friend!
- Bgood good good
- Cgoodgoodgood
- Dgood bye
- ENone ofthe above.
64What will be written to the standard output when the following program is run?
public class TrimTest{
public static void main( String args[ ] ) {
String blank = " " ; // one space
String line = blank + " hello" + blank + blank;
line. concat( " world" ) ;
String newLine = line. trim( ) ;
System. out. println( ( int) ( line. length( ) + newLine. length( ) ) ) ;
}
}- A25
- B24
- C23
- D22
- ENone ofthe above.
65Given:
public class Employee{
String name;
public Employee( ) {
}
}Whi
ch ofthe following lines creates an Employee instance?- AEmployee e;
- BEmployee e = new Employee( ) ;
- CEmployee e = Employee. new( ) ;
- DEmployee e = Employee();
66What 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;
}c
atch( Exception e) {
return 3;
}f
inally{
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) ;
}
}- AIt will not compile.
- BIt will throw an exception at runtime.
- C800
- D200
- E400
67Which ofthe following are valid declarations:
- Aint a = b = c = 100;
- Bint a, b, c; a = b = c = 100;
- Cint a, b, c=100;
- Dint a=100, b, c;
- Eint a= 100 = b = c;
68What will be the output ofthe following program (excluding the quotes)?
public class SubstringTest{
public static void main( String args[ ] ) {
String String = " string isa string" ;
System. out. println( String. substring( 3, 6) ) ;
}
}- AIt will not compile.
- B"ing is"
- C"ing "
- D"ing " (There is a space after g)
- ENone ofthe above.
69What will the following code print?
public class TestClass{
int x = 5;
int getX( ) { return x; }
public static void main( String args[ ] ) throws Exception{
TestClass tc = new TestClass( ) ;
tc. looper( ) ;
System. out. println( tc. x) ;
}p
ublic void looper( ) {
int x = 0;
while( ( x = getX( ) ) ! = 0 ) {
for( int m = 10; m>=0; m--) {
x = m;
}
}
}
}- AIt will not compile.
- BIt will throw an exception at runtime.
- CIt will print 0.
- DIt will print 5.
- ENone ofthese.
70What will the following code print when compiled an
class Base{
void methodA( ) {
System. out. println( " base - MethodA" ) ;
}
}c
lass Sub extends Base{
public void methodA( ) {
System. out. println( " sub - MethodA" ) ;
}p
ublic void methodB( ) {
System. out. println( " sub - MethodB" ) ;
}p
ublic static void main( String args[ ] ) {
Base b=new Sub( ) ; //1
b. methodA( ) ; //2
b. methodB( ) ; //3
}
}- Asub - MethodA and sub - MethodB
- Bbase - MethodA and sub - MethodB
- CCompile time error at //1
- DCompile time error at //2
- ECompile time error at //3
71What is the correct parameter specification for the standard main method?
- Avoid
- BString[ ] args
- CStrings args[ ]
- DString args
- EString args[ ]
72A java source file contains the following code:
interface I {
int getI( int a, int b) ;
}i
nterface J{
int getJ( int a, int b, int c) ;
}a
bstract class MyIJ implements J , I { }
class MyI{
int getI( int x, int y) { return x+y; }
}i
nterface K extends J{
int getJ( int a, int b, int c, int d) ;
}I
dentify the correct statements:- AIt will fail to compile because ofMyIJ
- BIt will fail to compile because ofMyIJ and K
- CIt will fail to compile because ofK
- DIt will fail to compile because ofMyI and K
- EIt will fail to compile because ofMyIJ, K, and MyI
- FIt will compile without any error.
73Which ofthe following statements is correct?
Correct Option is : E
A. new, delete and goto are keywords in the Java language- Anew, delete and goto are keywords in the Java language
- Btry, catch and thrown are keywords in the Java language
- Cstatic, unsigned and long are keywords in the Java language
- Dexit, class and while are keywords in the Java language
- Ereturn, goto and default are keywords in the Java language
74Given the following code, which method declarations can be inserted at line 1 without
any problems?
public class OverloadTest{
public int sum( int i1, int i2) { return i1 + i2; }
// 1
}- Apublic int sum( int a, int b) { return a + b; }
- Bpublic int sum( long i1, long i2) { return ( int) i1; }
- Cpublic int sum( int i1, long i2) { return ( int) i2; }
- Dpublic long sum( long i1, int i2) { return i1 + i2; }
- Epublic long sum( int i1, int i2) { return i1 + i2; }
75What will the following program print?
class Test{
public static void main( String args[ ] ) {
int var = 20, i=0;
do{
while( true) {
if( i++ > var) break;
}
} while( i<var--) ;
System. out. println( var) ;
}
}- A19
- B20
- C21
- D22
- EIt will enter an infinite loop.
76Given the following LOCs:
int rate = 10;
XXX amount = 1 - rate/100*1 - rate/100;
What can XXX be?- Aonly int or long
- Bonly long or double
- Conly double
- Ddouble or float
- Elong or double but not int or float.
- Fint, long, float or double
77Consider the classes shown below:
class A{
public A( ) { }
public A( int i) { System. out. println( i ) ; }
}c
lass B{
static A s1 = new A( 1) ;
A a = new A( 2) ;
public static void main( String[ ] args) {
B b = new B( ) ;
A a = new A( 3) ;
}s
tatic A s2 = new A( 4) ;
}Whi
ch is the correct sequence ofthe digits that will be printed when B is run?- A1 ,2 ,3 4.
- B1 ,4, 2 ,3
- C3, 1, 2, 4
- D2, 1, 4, 3
- E2, 3, 1, 4
78An instance member ...
- Acan be a variable, a constant or a method.
- Bis a variable or a constant.
- Cbelongs to the class.
- Dbelongs to an instance ofthe class.
- Eis same as a local variable
79Given:
public class Square {
private double side = 0; // LINE 2
public static void main( String[ ] args) { // LINE 4
Square sq = new Square( ) ; // LINE 5
side = 10; // LINE 6
}
}Wha
t can be done to make this code compile and run?- Areplace // LINE 2 with:
private int side = 0; - Breplace // LINE 2 with:
public int side = 0; - Creplace // LINE 5 with:
double sq = new Square( ) ; - Dreplace // LINE 6 with:
sq. side = 10;
- Areplace // LINE 2 with:
80Which ofthe following can be thrown using a throw statement?
- AEvent
- BObject
- CThrowable
- DException
- ERuntimeException
Informações
- Descrição
- Exam Mock EthuWare -OCA-SE-7-I Quiz sobre 02, criado por Wong Fei Hung em 30-07-2017.
- Autor
- Wong Fei Hung
- Idioma
- Português
- Visualizações
- 0
- Etiquetas
- Avaliação
- Ainda sem avaliações Avalie este quiz