Você está na página 1de 38

1 Which of the following expressions are legal and which are not?

Give your
reasons in each case.

A. byte B=(byte)x; x boolean


B. x=(char)(x-y); x char
C. f(x&&y); x char
D. x=(char)(x>>1); x char
E. boolean b=(boolean) x; x char

2 Which code fragments would correctly identify the number of arguments


passed via the command line to a Java application, excluding the name of the class
that is being invoked? What is wrong with the code fragments that are incorrect and
what errors do they produce?

A. int count = args.length;


B. int count = args.length - 1;
C. int count = 0;
while (args[count] != null)
count ++;
D. int count=0;
while (!(args[count].equals("")))
count ++;

3 What is the difference between the following 2 fragments of code and which
couldcause a runtime exception?

A. if (s!=null) & (s.length()>20)) {


System.out.println(s);
}
B. if (s!=null) && (s.length()>20)) {
System.out.println(s);
}

4 Which of the following code fragments are legal and which are not? Explain.

A. package sportinggoods;
class Ski {
void applyWax() {...}
}

package sportinggoods;
class DownhillSki extends Ski {
private applyWax() {...}
}
B. package Transport;
class Vehicle {
protected void Drive() {...}
}

package Transport;
class Car extends Vehicle {
public void Drive() {...}
}
C. In same file: class Vehicle {
public void Drive() {...}
}

class Car extends Vehicle {


private void Drive() {...}
}
D. In same file: class Ski {
private void applyWax() {...}
}

class DownhillSki extends Ski {


void applyWax() {...}
}

5 Which of the following code fragments are legal and which are not? Explain.

A. public class SportsTournament {


abstract void finalgame() {}
void kickoffgame() {...}
}
class WorldCup extends SportsTournament {
void finalgame() {...}
}
B. abstract class Animal {
public abstract void travel() {}
public abstract void feed() {}
}
class Mammal extends Animal
{
void travel() {...}
}
C. interface Animal {
void travel() {}
void feed() {}
}
class Mammal implements Animal {
void travel() {}
}
D. abstract public class Animal {
abstract void travel() {}
abstract void feed() {}
}
class Mammal extends Animal
{
void travel() {...}
void feed() {...}
} (2 things wrong with this !!)

6 Which of the following code fragments are legal and which are not? Explain.

E. float f=1.0;
int i=1;
byte b=i+f;
F. int i=5;
long l=7;
float f=i*l;
G. int i=5;
void calculate(float f) {...}
calculate(i);
H. byte b;
char c;
c='k';
b=c;
I. int i=1;
boolean negate(boolean b) {...}
negate(i);

7 Which of the following code fragments are legal and which are not? Explain.

A. class Fruit {
public Fruit() {...}
}
public class Orange extends Fruit {
public Orange() {...}
public static void main(String []args){
Fruit f=new Fruit();
Orange o=f;
}
}
B. class Fruit {
public Fruit() {...}
}
public class Orange extends Fruit {
public Orange() {...}
public static void main(String []args){
Orange o=new Orange();
Fruit f=o;
}
}
C. interface Fruit {
public Fruit();
}
public class Apple implements Fruit {
public Apple() {...}
public static void main(String []args){
Fruit f=new Fruit();
Apple a=f;
}
}
D. interface Fruit {
public Fruit();
}
public class Apple implements Fruit {
public Apple() {...}
public static void main(String []args){
Apple a=new Apple();
Fruit f=a;
}
}
E. interface Fruit {
public Fruit();
}
class Apple implements Fruit {
public Apple() {...}
}
class Orange implements Fruit {
public Orange() {...}
}
public class MyFruit {
public static void main(String []args){
Orange o=new Orange();
Fruit f=o;
Apple a=f;
}
}

8 Which of the following code fragments are legal and which are not? Explain.
E. int i;
for (i=5,int j=10; i<10;j--) { }
F. int i,j;
for (i=0,j=10;i<10, j>0;i++,j--) { }
G. int i,k;
for (i=0,k=9;(i<10 && k>0);i++,j--) { }
H. int i,j;
for (i=0;j=10;i<10;i++,j--) { }
9 Which of the following code fragments are legal and which are not? Explain.

1. abstract class Shape {


Shape() throws ZeroSizeException {}
abstract void draw() throws ZeroSizeException;
}
class Circle {
void draw() throws ZeroSizeException {..}
void getCircumference() throws ZeroSizeException, NegativeRadiusException{..}
2. abstract class Shape {
Shape() throws ZeroSizeException {}
abstract void draw() throws ZeroSizeException;
}
class Circle {
void draw() {..}
void getCircumference() throws ZeroSizeException, NegativeRadiusException {..}
}
3. abstract class Shape {
Shape() throws ZeroSizeException {}
abstract void draw() throws ZeroSizeException;
}
class Circle {
void draw() throws ZeroSizeException, NegativeRadiusException {..}
}
4. class Mammal {
Mammal() throws ColdBloodedException{}
void GiveBirth() {}
}
interface CanFly {
void fly();
}
class Bat extends Mammal implements CanFly {
Bat() throws ColdBloodedException, NoWingsException {}
void GiveBirth() {...}
}
5. class Mammal {
Mammal() throws ColdBloodedException{}
void GiveBirth() {}
}
interface CanFly {
void fly();
}
class Bat extends Mammal implements CanFly {
Bat() throws NoWingsException {}
void GiveBirth() {...}
void fly() {...}
}
6. class ColdBloodedException extends Exception {}
class LaysEggsException extends Exception {}
class Mammal {
Mammal() throws ColdBloodedException, LaysEggsException{...}
void GiveBirth() {}
}
class VariableBodyTemperature extends ColdBloodedException{}
interface CanFly {
void fly();
}
class Bat extends Mammal implements CanFly {
Bat() throws VariableBodyTemperature, NoWingsException {}
void GiveBirth() {...}
void fly() {...}
}

(Note: This has nothing to do with Exceptions)

10 Which of the following code fragments are legal and which are not? Explain.
public class BaseClass {
BaseClass() {...}
public void method() throws IOException {
...
}
}

1. public class CaseOne extends BaseClass {


CaseOne() {...}
public void method() throws IOException {
...
}
}
2. public class CaseTwo extends BaseClass {
CaseTwo() {...}
public void method() {
...
}
}
3. public class CaseThree extends BaseClass {
CaseThree() {...}
public void method() throws EOFException,MalformedURLException{
...
}
}
4. public class CaseFour extends BaseClass {
CaseFour() {...}
public void method() throws IOException, IllegalAccessException{
...
}
}
5. public class CaseFive extends BaseClass {
CaseFive() {...}
public void method() throws Exception {
...
}
}
11 For each of the following code fragments, please indicate which has an
overridden vs. overloaded method and explain why.

1. abstract class Shape {


public Shape ();
void draw();
}
class Circle extends Shape {
public Circle() { ...}
void draw(double x,double y, double radius) {...}
}
2. abstract class Shape {
public Shape ();
void draw();
}
class Circle extends Shape {
public Circle() { ...}
void draw() {...}
}
3. abstract class Mammal {
public Mammal();
Mammal giveBirth();
}
class Dog extends Mammal {
public Dog () {...}
Dog giveBirth() {...}
}
4. abstract class Mammal {
public Mammal();
Mammal giveBirth();
}
class Dog extends Mammal {
public Dog () {...}
Dog giveBirth(int no_of_pups) {...}
}

12 Which of the following code fragments are legal and which are not? Explain.

1. class MusicWork {
MusicWork(String s) {
System.out.println("The name of this work is" + s);
}
class ClassicalWork extends MusicWork {
ClassicWork(String s, String composer) {
System.out.println("The composer is " + composer);
}
2. class MusicWork {
MusicWork(String s) {
System.out.println("The name of this work is" + s);
}
class ClassicalWork extends MusicWork {
ClassicWork(String s, String composer) {
super(s);
System.out.println("The composer is " + composer);
}
3. class MusicWork {
MusicWork(String s) {
System.out.println("The name of this work is" + s);
}
class ClassicalWork extends MusicWork {
ClassicWork(String s, String composer) {
System.out.println("This is a work of classical music");
System.out.println("The composer is " + composer);
super(s);
}
4. class MusicWork {
MusicWork() {
System.out.println("This is a work of music");
}
MusicWork(String name) {
this();
System.out.println("The name of this work is" + name);
}

5. class MusicWork {
MusicWork() {
System.out.println("This is a work of music");
}
MusicWork(String name) {
this();
System.out.println("The name of this work is" + name);
}
MusicWork(String composer) {
this();
System.out.println("The composer of this work is" + composer);
}
6. class MusicWork {
MusicWork() {
System.out.println("This is a work of music");
}
MusicWork(String name) {
System.out.println("The name of this work is" + name);
this();
}

13 Which of the following code fragments are legal and which are not? Explain.

A) public class Outer {


String a;
public class Inner {
String b;
public void InnerMethod() {
System.out.println("Enclosing a is " + a);
System.out.println("y i s " + y);
}
}
public void CreateInner() {
Inner i=new Inner();
i.InnerMethod();
}
}

B) public class Outer {


String a;
public class Inner {
String b;
public void InnerMethod() {
System.out.println("Enclosing a is " + a);
System.out.println("b is " + b);
}
}
public void CreateInner() {
Outer.Inner i=new Outer.Inner();
i.InnerMethod();
}
}

C) public class Outer {


String a;
int k=1;
public static class Inner {
String b;
public void InnerMethod() {
System.out.println("Enclosing a is " + a);
System.out.println("b i s " + b);
}
}
public void CreateInner() {
Outer.Inner i=new Outer.Inner();
i.innerMethod();
System.out.println("This is the value of k: " + k);
}
}
D) public class Outer {
static String a;
static int k;
public Outer {
k++;
}
public class Inner {
String b;
public void InnerMethod() {
System.out.println("Enclosing a is " + a);
System.out.println("b is " + b);
}
public void CreateInner() {
Outer.Inner i=new Outer.Inner();
i.InnerMethod();
System.out.println("This is the instance no: " + k);
}
}
}

14 Which of the following code fragments are legal and which are not? Explain.

1. Character c= new Character("x");


2. int primitive=1234;
Integer wrappedInt=new Integer(primitive);
3. int primitiveInt=123;
Float wrappedFloat=new Float(primitiveInt);
4. Vector v=new Vector();
for (int i=0;i<10;i++)
v[i]=i;
5. Long wLong=new Long("here");
6. Boolean wBoolean=new Boolean("junk");

15 Given the following code fragments, which of the following a,b,c,d are true?

1. String s1="Compare me";


String s2="Compare me";
if (s1.equals(s2)){
System.out.println("Success");
} else {
System.out.println("Failure");
}

2. String s1="Compare me";


String s2="Compare me";
if (s1==s2){
System.out.println("Success");
} else {
System.out.println("Failure");
}

a) Both I and II print Failure


b) Both I and II print Success
c) I print Success, II prints Failure
d) I prints Failure, II prints Success

16 What will be the output when you compile and execute the following program.
01: class Base
02:{
03: final int MAX_SIZE;
04: Base(){
05: MAX_SIZE = 10;
06: }
07:
08: void test() {
09: MAX_SIZE++;
10: System.out.println("Base.test()");
11: }
12:
13:}

Select most appropriate answer.

a) Compilaton Error at Line 09: Can't assign a second value to a blank final
variable: MAX_SIZE

b) Compilation Error at Line 03: Can't define a blank final variable: MAX_SIZE

c) Compilation Error at Line 05: Can't assign a value to a blank final variable:
MAX_SIZE

d) No errors

17 Which statement is true about a non-static inner class?

A.It must implement an interface.

B.It is accessible from any other class.

C.It can only be instantiated in the enclosing


class.

D.It must be final if it is declared in a method


scope.

E.It can access private instance variables in the


enclosing object.

18 Which is the advantage of encapsulation?

A.Only public methods are needed.


B.No exceptions need to be thrown from any
method.

C.Making the class final causes no


consequential changes to other code.

D.It changes the implementation without


changing the interface and causes no
consequential changes to other code.

E.It changes the interface without changing the


implementation and causes no consequential
changes to other code.

19 When you compile and execute the following program which lines will give
compilation and/or runtime errors.

01:public class Base{


02: private void test() {
03:
04: int i = 3;
05: int a[] = { 0, 1 };
06: Integer b[] = new Integer[i*i];
07: Long c[] = { new Long(1L) , new Long(2L) };
08: int d[] = new int[] { 10, 20 };
09: int e[] = new int[2] { 10, 20 };
10: Integer f[] = new Integer[i];
11:
12:}
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18:}

a) Invalid array initialization at line 05


b) Invalid array size at line 06. A local variable cannot be used as array size
c) Invalid array initialization at line 07
d) Invalid array initialization at line 08. Cannot new and initialize an array at same
time
e) Error at line 09. Cannot specify array size and initialize an array at same time
f) Invalid array size at line 10. A local variable cannot be used as array size

20 What will be the output when you compile and execute the following program.
class Base
{
protected void test() {
System.out.println("Base.test()");
}

public class Child extends Base {

void test() {
super.test(); //Call the parent method
System.out.println("Child.test()");
}

static public void main(String[] a) {


new Child().test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Compilation error. Cannot make the access modifier more restrictive for test()

d) Compilation error. Cannot call super.test() since parent method is protected

21 At what point is the object anObj available for garbage collection.

01: public class Base{


02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }

Select most appropriate answer

a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11

22 What will be the output when you compile and execute the following program.

public class Base{

private void test() {


System.out.println(!6);
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer.

a) 0
b) false
c) -6
d) Compilation Error.Incompatible type for !. Can't convert int to boolean.
e) !6

23 What will be the output when you compile and execute the following program.

class Base
{
static void test() {
System.out.println("Base.test()");
}
}

public class Child extends Base {

static void test() {


System.out.println("Child.test()");
}

static public void main(String[] a) {


Child anObj = new Child();
anObj.test();

Base baseObj = (Base)anObj;


baseObj.test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Base.test()
Base.test()

d) Compilation error. Cannot override a static method

e) Runtime error. Cannot override a static method

24 What will be the output when you compile and execute the following program.

1:public class Base{


2:
3: int array[];
4:
5: private void test() {
6: System.out.println("Value is: " + array[0]);
7: }
8:
9: static public void main(String[] a) {
10: new Base().test();
11: }
12:
}

Select most appropriate answer.

a) Compile time error. Variable array may not have been initialized.
b) Runtime error. Variable array[0] may not have been initialized.
c) Runtime error. java.lang.NullPointerException at line No 6

25 Which of the following statements are true?

a) An abstract method cannot be final


b) An abstract method cannot be static
c) An abstract method cannot be private
d) An abstract method must be specified inside an abstract class only
e) An abstract method cannot throw any exceptions
Select all valid answers.

26 What will be the output when you compile and execute the following program.

public class Base{

private void test() {


System.out.println(6 + 6 + "(Result)");
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer.

a) 66
b) 66(Result)
c) Runtime Error.Incompatible type for +. Can't convert an int to a string.
d) Compilation Error.Incompatible type for +. Can't add a string to an int.
e) 12(Result)

27 Given the following code,

Float f = new Float(0.9F);


Float g = new Float(0.9F);
Double d = new Double(0.9);

Which of the following conditions are valid and will return true?
a. (f==g)
b. (g==d)
c. (f.equals(new Float(0.9F))
d. (f.equals(d))
e. (f.equals(g))

28 What will be the output when you compile and execute the following program.

public class Base{

static private int i;

static public void main(String[] a) {


System.out.println("Value is: " + i);
}

Select most appropriate answer(s).

a) Value is: 0
b) Compile time error. Can't access the private variable i defined in class Base.
c) Compile time error. Can't make a static reference to nonstatic variable i in class
Base.
d) Runtime error. Variable i is uninitialized
e) Compile time error. Variable i is uninitialized
f) None

29 For what value of i, the following program will output "Less than 20"

public class Base{

private void test() {

int i ;

if(i < 10)


System.out.println("Less than 10");
else
if ( i < 20)
System.out.println("Less than 20");
else
System.out.println("Not less than 10");

}
static public void main(String[] a) {
new Base().test();
}

Select most appropriate answer.

a) 0
b) 10
c) 11
d) 19.999;
e) (int)19.99
f) None

30 What will be the output when you compile and execute the following program.

class Base
{
void test() {
System.out.println("Base.test()");
}

public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {


Child anObj = new Child();
Base baseObj = (Base)anObj;
baseObj.test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Base.test()
Child.test()

c) Base.test()

d) Child.test()

31 What will be the output when you compile and execute the following program.

class Base
{
Base(int i) { test(); }

void test() {
System.out.println("Base.test()");
}

public class Child extends Base {

Child(int i) { test (); }

Child(float f) { this ((int)f); }

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {


new Child(10.8f).test();
}
}

Select most appropriate answer.

a) Base.test()
Child.test()

b) Compilation Error: No default constructor ( constructor matching Base())


found in class Base.

c) Runtime Error: No default constructor ( constructor matching Base())


found in class Base.

d) Child.test()
32 A sample question provided by Sun

Which are keywords in Java?

A.NULL

B.sizeof

C.friend

D.extends

E.synchronized

33 What will be the output when you compile and execute the following program.

public class Base {

void test() {

float f; // what should be the value of f

if (Math.abs(f) == Math.round(f))
System.out.println("Math.abs(f) == Math.round(f)");

if (Math.ceil(f) == Math.round(f))
System.out.println("Math.ceil(f) == Math.round(f)");

if (Math.floor(f) == Math.round(f))
System.out.println("Math.floor(f) == Math.round(f)");

if (Math.floor(f) == Math.ceil(f))
System.out.println("Math.floor(f) == Math.ceil(f)");

static public void main(String[] a) {


new Base().test();
}
}

Select all valid answers.


a) 1.0f
b) -0
c) .999f
d) None

34 What will be the output when you compile and execute the following program.

public class Base{

private void test() {


System.out.println(6 & 3);
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer.

a) true
b) false
c) 9
d) Compilation Error
e) 2

35 What will be the output when you compile and execute the following program.

01: class Base


02:{
03: static final int MAX_SIZE;
04: Base(){
05: MAX_SIZE = 10;
06: }
07:
08: void test() {
09: MAX_SIZE++;
10: System.out.println("Base.test()");
11: }
12:
13:}

Select all valid answers.


a) Compilation Error at Line 03: Blank final variable 'MAX_SIZE' may not have
been
initialized. It must be assigned a value in an initializer, or in every constructor.
b) Compilation Error at Line 05: Can't assign a second value to a blank final
variable:
c) Compilation Error at Line 09: Can't assign a second value to a blank final
variable:
d) No errors

36 A sample question provided by Sun

Which correctly create an array of five empty Strings?

A.String a [] = new String [5];


for (int i = 0; i < 5; a[i++] = "");

B.String a [] = {"", "", "", "", ""};

C.String a [5];

D.String [5] a;

E.String [] a = new String [5];


for (int i = 0; i < 5; a[i++] = null);

37 What will be the output when you compile and execute the following program.

public class Base{

private int j;

private void test() {


int i;
System.out.println("Value is: " + i);
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer(s).

a) Value is: 0
b) Runtime error. Variable i may not have been initialized.
c) Compile time error. Variable i may not have been initialized.
d) None
e) Error.Cannot access the private method test() from main()

38 What will be the output when you compile and execute the following program.

public class Base{

private void test() {


System.out.println(2^6);
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer.

a) 0
b) 2
c) 32
d) Compilation Error.Incompatible type for ^. Can't convert int to boolean.
e) 8
f) 4

39 Which of the following lines will compile without warning or error.

Select all valid answers.

a) float f=0;
b) char c="0";
c) byte b=157;
d) boolean f=null;
e) int i=10;

40 What will be the output when you compile and execute the following program.

1:public class Base{


2:
3:
4: private void test() {
5: boolean f;
6: System.out.println("Value is: " + f);
7: }
8:
9: static public void main(String[] a) {
10: new Base().test();
11: }
12:
}

Select most appropriate answer.

a) Compile time error. Variable f may not have been initialized.


b) Runtime error. Variable f may not have been initialized.
c) Value is: null
d) Runtime error. java.lang.NullPointerException at line No 6
e) Value is: false
e) Value is: true

41 What will be the output when you compile and execute the following program.

public class Base{

private int i;

static public void main(String[] a) {


System.out.println("Value is: " + new Base().i);
}

Select most appropriate answer(s).

a) Value is: 0
b) Compile time error. Can't access the private variable i defined in class Base.
c) Compile time error. Can't make a static reference to nonstatic variable i in class
Base.
d) Runtime error. Variable i is uninitialized
e) Compile time error. Variable i is uninitialized
f) None

42 Which of the following valid statements

Select all valid answers.

a) all classes extend Object by default


b) all classes implement Cloneable by default
c) In Java a class cannot extend more than one class.
d) In Java a class cannot extend a class and implement another interface at same time
e) In Java a class cannot implement a class.

43 A sample question provided by Sun

Which are correct class declarations? Assume in each case that the text constitutes
the entire contents of
a file called Fred.java on a system with a case-significant file system.

A.public class Fred {


public int x = 0;
public Fred (int x) {
this.x = x;
}
}

B.public class fred


public int x = 0;
public fred (int x) {
this.x = x;
}
}

C.public class Fred extends MyBaseClass, MyOtherBaseClass {


public int x = 0;
public Fred (int xval) {
x = xval;
}
}

D.protected class Fred {


private int x = 0;
private Fred (int xval) {
x = xval;
}
}

E.import java.awt.*;
public class Fred extends Object {
int x;
private Fred (int xval) {
x = xval;
}
}
44 What will be the output when you compile and execute the following program.

public class Base{

private void test() {


System.out.println(6 >> 33);
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer.

a) 6
b) 1
c) 3
d) Compilation Error.Incompatible type for >>. Cannot be greater than 32
e) –1

45 What will be the output when you compile and execute the following program.

public class Base{

private void test() {


System.out.println(6 >> 3);
}

static public void main(String[] a) {


new Base().test();
}

Select most appropriate answer.

a) true
b) false
c) 0
d) Compilation Error.Incompatible type for >>. Can't convert int to boolean.
e) –1

46 Given the following class definition:


class A {
protected int i;
A(int i) {
this.i = i;
}
}

47 Which of the following would be a valid inner class for this class?

Select all valid answers.

a) class B {
}

b) class B extends A {
}

c) class B {
B() {
System.out.println("i = " + i);
}
}

d) class B {
class A {
}
}

e) class A {
}

48 Which of the following class definitions defines a legal


abstract class?

Select all right answers.

a)
class Animal { abstract void growl(); }

b)
abstract Animal {abstract void growl();}

c)
class abstract Animal {abstract void growl();}

d)
abstract class Animal {abstract void growl();}

e)
abstract class Animal {abstract void growl()
{System.out.println("growl");}}

49 Given the following code:


class Tester {

public static void main(String[] args) {


CellPhone cell = new CellPhone();

cell.emergency();
}

class Phone {

final void dial911() {


// code to dial 911 here . . .
}

class CellPhone extends Phone {

void emergency() {
dial911();
}

What will happen when you try to compile and run the Tester class?

a.The code will not compile because Phone is not also declared as
final.
b.The code will not compile because you cannot invoke a final
method from a subclass.
c.The code will compile and run fine.
d.The code will compile but will throw a NoSuchMethodException
when Tester is run.
e.Phone and CellPhone are fine, but Tester will not compile because
it cannot create an instance of a class that derives from a class
defining a final method.

50 Which assignments are legal?

Select all valid answers.

a.long test = 012;


b.float f = -412;
c.int other = (int)true;
d.double d = 0x12345678;
e.short s = 10;

51 Given this class definitions:


abstract class Transaction implements Runnable
{}

class Deposit extends Transaction {

protected void process() {


addAmount();
}

void undo(int i) {
System.out.println("Undo");
}

What will happen if we attempted to compile the code?

Select the one right answer.

a.This code will not compile because the parameter i is not used in
undo().
b.This code will not compile because there is no main() method.
c.This code will not compile because Deposit must be an
abstract class.
d.This code will not compile because Deposit is not declared
public.
e.Everything will compile fine.
52 What will appear in the standard output when you run the
Tester class?
class Tester {

int var;

Tester(double var) {
this.var = (int)var;
}

Tester(int var) {
this("hello");
}

Tester(String s) {
this();

System.out.println(s);
}

Tester() {
System.out.println("good-bye");
}

public static void main(String[] args) {


Tester t = new Tester(5);
}

a.nothing
b."hello"
c.5
d."hello" followed by "good-bye"
e."good-bye" followed by "hello"

53 There are a number of labels in the source code below.


These are labeled a through j. Which label identifies the earliest point
where, after that line has executed, the object referred to by the variable
first may be garbage collected?
class Riddle {

public static void main(String[] args) {


String first, second;
String riddle;

if (args.length < 2)

return; a: first = new


String(args[0]);

b: second = new String(args[1]);

c: riddle = "When is a " + first;

d: first = null;

e: riddle += " like a " + second +


"?";

f: second = null;

g: System.out.println(riddle);

h: args[0] = null;

i: args[1] = null;

j:
}
}

Select the one right answer.

a.d:
b.e:
c.h:
d.i:
e.j:

54 What will happen when you try to compile and run the
following program?
class Car {

int milesPerGallon;

int index;
Car(int mpg) {
milesPerGallon = mpg;

index = 0;
}

Car() {

public static void main(String[] args) {


int index;

Car c = new Car(25);

if (args.length > 0)
if (args[index].equals("Hiway"))
milesPerGallon*= 2;
System.out.println("mpg: " +
milesPerGallon);
}
}

Select the one right answer.

a.The code compiles and displays "mpg: 50" if the command-line


argument is "Hiway". If the command-line argument is not
"Hiway", the code displays "mpg: 25".
b.The code compiles and displays "mpg: 50" if the command-line
argument is "Hiway". If the command-line argument is not
"Hiway", the code throws an ArrayIndexOutOfBoundsException.
c.The code does not compile because the automatic variable named
index has not been initialized.
d.The code does not compile because milesPerGallon has not
been initialized.
e.The code does not compile because the no-args constructor is not
written correctly.

55 What will happen when you compile and run this program:
class Array {

public static void main(String[] args) {


int length = 100;

int[] d = new int[length];


for (int index = 0; index < length;
index++)
System.out.println(d[index]); }
}

Select the one right answer.

a.The code will not compile because the int[] array is not
declared correctly.
b.The code will compile but will throw an
IndexArrayOutOfBoundsException when it runs and nothing will
appear in the standard output.
c.The code will display the numbers 0 through 99 in the standard
output, and then throw an IndexOutOfBoundsException.
d.The code will compile but the println() method will throw a
NoSuchMethodException.
e.This code will work fine and display 100 zeroes in the standard
output.

56 Given these class definitions:


class Superclass { }

class Subclass1 extends Superclass { }

and these objects:


Superclass a = new Superclass();

Subclass1 b = new Subclass1();

which of the following explains the result of the statement:


a = b;

Select the one right answer.

a.Illegal at compile time


b.Legal at compile time but possibly illegal at runtime
c.Definitely legal at runtime

57 Given these class definitions:


class Superclass { }

class Subclass1 extends Superclass { }

class Subclass2 extends Superclass { }


and these objects:
Superclass a = new Superclass();

Subclass1 b = new Subclass1();

Subclass2 c = new Subclass2();

which of the following explains the result of the statement:


b = (Subclass1)c;

Select the one right answer.

a.Illegal at compile time


b.Legal at compile time but possibly illegal at runtime
c.Definitely legal at runtime

58 How can you change the break statement below so


that it breaks out of both the inner and middle loops and continues
with the next iteration of the outer loop?

outer: for (int x = 0; x < 3; x++) {

middle: for (int y = 0; y < 3; y++) {


inner: for (int z = 0; z < 3; z++) {
if (arr(x, y, z) == targetValue)
break;
}
}

Select the one right answer.

a.break inner;
b.break middle;
c.break outer;
d.continue;
e.continue middle;
59 What is the result of executing the following code:
class Test {

public static void main(String[] args) {


Test t = new Test();

t.test(1.0, 2L, 3);


}
void test(double a, double b, short c) {
System.out.println("1");
}

void test(float a, byte b, byte c) {


System.out.println("2");
}

void test(double a, double b, double c) {


System.out.println("3");
}

void test(int a, long b, int c) {


System.out.println("4");
}

void test(long a, long b, long c) {


System.out.println("5");
}

Select the one right answer.

a.1
b.2
c.3
d.4
e.5

60 What access control keyword should you use to allow


other classes to access a method freely within its package, but to
restrict classes outside of the package from accessing that method?

Select all valid answers.

a.public
b.protected
c.private
d.do not supply an access control keyword

61 Which label name(s) are illegal?

Select all valid answers.


a.here:
b._there:
c.this:
d.that:
e.2to1odds:

62 Given these code snippets:


Boolean b1 = new Boolean(true);

Boolean b2 = new Boolean(true);

Which expressions are legal Java expressions that return true?

Select all valid answers.

a.b1 == b2
b.b1.equals(b2)
c.b1 & b2
d.b1 | b2
e.b1 && b2
f.b1 || b2

63 What will happen when you attempt to compile and run the
following program by passing the Test class to the Java interpreter?
class Test {

public static void main() {


System.out.println("hello");
}

Select the one right answer.

a.The program does not compile because main() is not defined


correctly.
b.The program compiles but when you try to run the interpreter complains
that it cannot find the main() method it needs to run.
c.The program compiles but you cannot run it because the class is not
declared as public.
d.The program compiles and runs without an error but does not display
anything in the standard output.
e.The program compiles and displays "hello" in the standard output when
you run it.

64 What is the result of invoking main() for the classes D and E?


class D {

public static void main(String[] args) {


String s1 = new String("hello");

String s2 = new String("hello");

if (s1.equals(s2))
System.out.println("equal");
else
System.out.println("not equal");
}

class E {

public static void main(String[] args) {


StringBuffer sb1 = new
StringBuffer("hello");

StringBuffer sb2 = new


StringBuffer("hello");

if (sb1.equals(sb2))
System.out.println("equal");
else
System.out.println("not equal");
}

Select the one right answer.

a.D: equal; E: equal


b.D: not equal; E: not equal
c.D: equal; E: not equal
d.D: not equal; E: not equal
e.nothing appears in the standard output for either class
65 Which statements about garbage collection are true?

Select all valid answers.

a.You can directly free the memory allocated by an object.


b.You can directly run the garbage collector whenever you want to.
c.The garbage collector informs your object when it is about to be garbage
collected.
d.The garbage collector reclaims an object’s memory as soon as it
becomes a candidate for garbage collection.
e.The garbage collector runs in low-memory situations.

Você também pode gostar