Question 1
How many String objects are created in the following code?
1. String A, B, C;
2. A = “1234”;
3. B = A;
4. C = A + B;
a. 1
b. 2
c. 3
d. 4
Where are they created?
____it will create three string objects _____________________________________________________________
_________________________________________________________________
_________________________________________________________________
Question 2
Which of the following versions of initialising a char variable would cause a compiler error? [Check all correct answers]
a. char c = -1;
b. char c = ‘\u00FF’;
c. char c = (char) 4096;
d. char c = 4096L;
e. char c = ‘c’;
f. char c = “c”;
Question 3
What happens when you try to compile and run the following code?
1. public class EqualsTest {
2. public static void main(String args[]) {
3. Long LA = new Long ( 7 );
4. Long LB = new Long ( 7 );
5. if (LA == LB)
6. System.out.println(“Equal”);
7. else System.out.println(“Not Equal”);
8. }
9. }
a. The program compiles but throws a runtime exception in line 5.
b. The program compiles and prints “Equal”.
c. The program compiles and prints “Not Equal”.
Question 4
What would happen if you tried to compile and run the following code?
1. public class EqualsTest {
2. public static void main(String args[]) {
3. Long L = new Long ( 7 );
4. if (L.equals( 7L ))
System.out.println(“Equal”);
5. else System.out.println(“Not Equal”);
6. }
7. }
a. The program would compile and print “Equal”.
b. The program would compile and print “Not Equal”.
c. The compiler would object to line 4.
d. A runtime cast error would occur at line 4.
Question 5
What would happen if you tried to compile and run the following code?
1. public class EqualsTest {
2. public static void main(String args[]) {
3. Object A = new Long ( 7 );
4. Long L = new Long ( 7 );
5. if (A.equals( L ))
6. System.out.println(“Equal”);
7. else System.out.println(“Not Equal”);
8. }
9. }
a. The program would compile and prints “Equal”.
b. The program would compile and prints “Not Equal”.
c. The compiler would object to line 5.
d. A runtime cast error would occur at line 5.
Question 6
Given the following listing of the Widget class:
1. class Widget extends Thingee {
2. static private int widgetCount = 0;
3. public String wName;
4. int wNumber;
5. static synchronized int addWidget() {
6. widgetCount++;
7. wName = “I am Widget # ” + widgetCount;
8. return widgetCount;
9. }
10. public Widget() {
11. wNumber = addWidget();
12. }
13. }
What happens when you try to compile the class and use multiple Widget objects in a program?
a. The class compiles and each Widget will get a unique wNumber and wName reflecting the order in which the Widgets were created.
b. The compiler objects to line 7.
c. The class compiles, but a runtime error related to the access of the variable wName occurs in the addWidget method.
Question 7
Trying to compile the following source code produces a compiler warning to the effect that the variable tmp may not have been initialized.
1. class Demo {
2. String msg = “Type is ”;
3. public void showType ( int n ) {
4. String tmp;
5. if ( n > 0 ) tmp = “positive”;
6. System.out.println( msg + tmp );
7. }
8. }
Which of the following changes would eliminate this warning? [Check all correct answers]
a. Make line 4 read
4. String tmp = null;
b. Make line 4 read
4. String tmp = “”;
c. Insert a line following line 5
6. else tmp = “not positive”;
d. Remove line 4 and insert a new line after 2 so tmp becomes a member variable instead of a local variable in showType.
3. String tmp;
Question 8
The following method definition is designed to parse and return an integer from an input string that is expected to look like “nnn, ParamName”. In the event of NumberFormatException, the method is to return –1.
1. public int getNum( String S ) {
2. try {
3. String tmp = S.substring( 0, S.indexOf( ‘,’ ));
4. return Integer.parseInt( tmp );
5. } catch ( NumberFormatException e ) {
6. System.out.println( “Problem in ” + tmp );
7. }
8. return –1;
9. }
What happens when you try to compile this code and execute the method with an input string that does not contain a comma separating the number from the text data?
a. A compiler error in line 6 prevents compilation.
b. The method prints the error message to standard output and returns –1.
c. A NumberFormatException is thrown in line 3.
d. A StringIndexOutOfBoundsException is thrown in line 3.
Question 9
Given the following class definition:
1. public class DerivedDemo extends Demo {
2. int M, N, L;
3. public DerivedDemo( int x, int y ) {
4. M = x; N = y;
5. }
6. public DerivedDemo( int x) {
7. super( x );
8. }
9. }
Which of the following constructor signatures must exist in the Demo class for DerivedDemo to compile correctly? [Check all correct answers]
a. public Demo( int a, int b)
b. public Demo( int c)
c. public Demo( )
Question 10
When programming a local inner class inside a method code block, which of the following statements is true?
a. The inner class will have access only to static variables in the enclosing class.
b. The inner class can use any variables declared in the method.
c. The inner class can use only local variables that are declared final.
d. The inner class can use only local variables that are declared static.
Question 11
You have been given the following programming problem with respect to a Java application. An existing class called AB now needs some additional functionality, which it is proposed to add with a nested inner class, ABC. You have been able to establish the following requirements:
1. There will probably be more than one AB instance active in the application, so the solution has to work no matter how many AB instances there are.
2. ABC will need to have access to instance methods and variables as well as static variable.
3. More than one method in AB will have access to a method of ABC.
Which configuration of a nested class is the best bet for this problem?
a. A static class
b. A member inner class
c. An inner class defined in an AB method
d. An anonymous inner class
Question 12
In the following code for a class in which methodA has an inner class, which variables would the statement in line 8 be able to use in place of XX? [Check all correct answers]
1. public class Base {
2. private static final int ID = 3;
3. private String name;
4. public void methodA( final int nn ) {
5. int serialN = 11;
6. class inner {
7. void showResult( ) {
8. System.outprintln( “Rslt = ” + XX );
9. }
10. } // end class inner
11. new inner( ).showResult( );
12. } // end methodA
13. }
a. The int ID in line 2
b. The String in line 3
c. The int nn in line 4
d. The int serialN in line 5
Question 13
What will happen when you try to compile the following code?
1. public void printArray( Object x ) {
2. if ( x instanceof int[ ] ) {
3. int[ ] n = ( int[ ] ) x;
4. for ( int i = 0; i < n.length; i++ ) {
5. System.out.println( “integers = ” +
n[ i ] );}
6. }
7. if ( x instanceof String[ ] ) {
8. System.out.println( “Array of Strings” ;
9. }
10. }
a. It compiles without error.
b. The compiler objects to line 2 comparing an Object with an array.
c. The compiler objects to line 3 casting an Object to an array of int primitives.
d. The compiler objects to line 7 comparing an Object to an array of Objects.
Answer is : System.out.println( “Array of Strings” ;
) excepted
Question 14
Here is the class hierarchy showing the java.awt.event.ActionEvent class family tree:
java.lang.Object
|-- java.util.EventObject
|-- java.awt.AWTEvent
|-- java.awt.event.ActionEvent
Suppose you have the following code to count events and save the most recent event.
1. int evtCt = 0;
2. AWTEvent lastE;
3. public void saveEvent( AWTEvent evt ) {
4. lastE = evt;
5. evtCt++;
6. }
Which of the following calls of saveEvent would run without causing an exception? [Check all correct answers]
a. an AWTEvent object reference
b. an ActionEvent object reference
c. an EventObject object reference
d. null value
Question 15
Suppose you have two classes defined as follows:
class ApBase extends Object implements Runnable
class ApDerived extends ApBase implements Observer
Given two variables created as follows:
ApBase aBase = new ApBase( );
ApDerived aDer = new ApDerived( );
Which of the following Java statements will compile and execute without error? [Check all correct answers]
a. Runnable rn = aDer;
b. Runnable rn2 = (Runnable) aBase;
c. Observer ob = aBase;
d. Observer ob2 = (Observer) aBase;
Question 16
The following method is designed to convert an input string to a floating-point number while detecting a bad format.
1. public boolean strCvt( String s ) {
2. try {
3. factor = Float.valueOf( s ).floatValue( );
4. return true;
5. } catch ( NumberFormatException e ) {
6. System.out.println( “Bad number ” + s );
7. factor = Float.NaN;
8. } finally { System.out.println( “Finally” );
9. }
10. return false;
11. }
Which of the following descriptions of the results of various inputs to the method are correct? [Check all correct answers]
a. Input = “0.234” – Result: factor = 0.234, “Finally” is printed, true is returned.
b. Input = “0.234” – Result: factor = 0.234, “Finally” is printed, false is returned.
c. Input = null – Result: factor = NaN, “Finally” is printed, false is returned.
d. Input = null – Result: factor unchanged, “Finally” is printed, NullPointerException is thrown.
Question 17
Here is the class hierarchy of exceptions related to array index and string index errors:
Exception
+-- RuntimeException
+-- IndexOuOfBoundsException
+-- ArrayIndexOuOfBoundsException
+-- StringIndexOuOfBoundsException
Suppose you had a method X that could throw both array index and string index exceptions. Assuming that X does not have any try-catch statements, which of the following statements are correct? [Check all correct answers]
a. The declaration for X must include “throws ArrayIndexOuOfBoundsException, StringIndexOuOfBoundsException”.
b. If a method calling X catches IndexOuOfBoundsException, both array and string index exceptions will be caught.
c. If the declaration for X includes “throws IndexOuOfBoundsException”, any calling method must use a try catch block.
d. The declaration for X does not have to mention exceptions.
Question 18
You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows:
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your custom exceptions have constructors taking a String parameter. You have a method declared as follows:
int rateFlavor( Ingredient[ ] list ) throws BadTasteException
Which of the following shows a correct complete statement to throw one of your custom exceptions?
a. new SourException( “Ewww!” );
b. throws new SourException( “Ewww!” );
c. throw new SourException( “Ewww!” );
d. throw SourException( “Ewww!” );
Question 19
The GenericFruit class declares the following method:
public void setCalorieContent( float f )
You are writing a class Apple to extend GenericFruit and want to add methods that overload the method in GenericFruit. Which of the following would constitute legal declarations of overloading methods? [Check all correct answers]
a. protected float setCalorieContent( String s )
b. protected void setCalorieContent( float x )
c. public void setCalorieContent( double d )
d. public void setCalorieContent( String s ) throws NumberFormatException
Question 20
The GenericFruit class declares the following method to return a float number of calories in the average serving size:
public float aveCalories( )
Your Apple class, which extends GenericFruit, overrides this method. In a DietSelection class that extends Object, you want to use the GenericFruit method on an Apple object. Select the correct way to finish the statement in the following code fragment so the GenericFruit version of aveCalories is called using the gf reference, or select option d.
1. GenericFruit gf = new Apple( );
2. float cal = // finish this statement using gf
a. gf.aveCalories( );
b. ( ( GenericFruit ) gf ).aveCalories( );
c. gf.super.aveCalories( );
d. There is no way to call the GenericFruit method.
Question 21
The GenericFruit class declares the following method to return a float calculated from a serving size:
protected float calories( float serving )
In writing the Apple class that extends GenericFruit, you propose to declare an overriding method with the same parameter list and return type. Which access modifiers could you use with this overriding method? [Check all correct answers]
a. private
b. protected
c. public
d. “package”; that is, a blank access modifier
Question 22
Which of the following statements about the java.util.Vector and java.util.Hashtable classes are correct? [Check all correct answers]
a. A Vector can hold object references or primitive values.
b. A Vector maintains object references in the order they were added.
c. A Hashtable requires String objects as keys.
d. A Hashtable maintains object references in the order they were added.
e. Both Vector and Hashtable use synchronized methods to avoid problems due to more than one Thread trying to access the same collection.
Question 23
In the following class definitions, which are in separate files, note that the Widget and BigWidget classes are in different packages:
1. package conglomo;
2. public class Widget extends Object {
3. private int myWidth;
4. XXXXXX void setWidth( int n ) {
5. myWidth = n;
6. }
7. }
// the following is in a separate file
8. import conglomo.Widget;
9. public class BigWidget extends Widget {
10. BigWidget( ) {
11. setWidth( 204 );
12. }
13. }
Which of the following modifiers, used in line 4 instead of XXXXXX, would allow the BigWidget class to access the Widget.setWidth method (as in line 11)? [Check all correct answers]
a. private
b. protected
c. blank – that is, the method declaration would read void setWidth( int n )
d. public
Question 24
What happens on trying to compile and run the following code?
1. public class EqualsTest {
2. public static void main( String args[ ] ) {
3. float pear = 0.0F;
4. for(int i = 0; i<10; i++) pear = pear + 0.1F;
5. if (pear == 1.0F)
System.out.println( "Equal" );
6. if (pear != 1.0F)
System.out.println( "Not Equal" );
7. }
8. }
a. The program compiles and prints “Not Equal”.
b. The program compiles and prints “Equal”.
c. The compiler objects to line 3.
d. The compiler objects to using == with primitives in line 5.
Question 25
Given the following method in an application
1. public String setFileType( String fname ) {
2. int p = fname.indexOf( ‘.’ );
3. if( p > 0 ) fname = fname.substring( 0, p );
4. fname += “.txt”;
5. return fname;
6. }
and given that another part of the class has the following code
7. String TheFile = “Program.java”;
8. File F = new File( setFileType( TheFile ) );
9. System.out.println( “Created ” + TheFile );
what will be printed by the statement in line 9?
a. Created Program.java
b. Created Program.txt
c. Created Program.java.txt
Question 26
What happens when this method is called with an input of “Java rules”?
1. public String addOK( String S) {
2. S += “ OK!”;
3. return S;
4. }
a. The method will return “ OK!”;
b. A runtime exception will be thrown.
c. The method will return “Java rules OK!”.
d. The method will return “Java rules”.
Question 27
Which of the following code fragments are legal Java code? [Check all correct answers]
a. String A = “abcdefg”;
A -= “cde”;
b. String A = “abcdefg”;
A += “cde”;
c. Integer J = new Integer( 27 );
J -= 7;
d. Integer J = new Integer( 27 );
J--;
Question 28
Which statements regarding the following method definition are true?
boolean e() {
try {
assert false;
} catch (AssertionError ae) {
return true;
}
return false; // (1)
}
The code will fail to compile since catching AssertionError is illegal
The code will fail to compile since the return statement at (1) is unreachable
The method will return true under all circumstances
The method will return false under all circumstances
The method will return true if and only if assertions are enabled at runtime
Question 29
Which of the following statements are true?
The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
transient and volatile are Java modifiers.
Constructors are not inherited.
A static method may not be overriden to be non-static.
The size of a string can be retrieved using the length property.
Question 30
What will be written to the standard output when the following program is run?
class Base {
int i;
Base() { add(1); }
void add(int v) { i += v; }
void print() { System.out.println(i); }
}
class Extension extends Base {
Extension() { add(2); }
void add(int v) { i += v*2; }
}
public class Qd073 {
public static void main(String[] args) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
9
18
20
21
22
i lvoe u
ReplyDelete