sabato 30 marzo 2013

Q104


Given:

 

public class ScopeTest {

       int j, k;

 

       public static void main(String[] args) {

             new ScopeTest().doStuff();

       }

 

       void doStuff() {

             int x = 5;

             doStuff2();

             System.out.println("x");

       }

 

       void doStuff2() {

             int y = 7;

             System.out.println("y");

             for (int z = 0; z < 5; z++) {

                    System.out.println("z");

 

                    System.out.println("y");

 

             }

       }

}

 

Which two items are fields?

A.
j

B.
k

C.
x

D.
y

E.
z

 La risposta è AB. Infatti j e k sono campi dell'oggetto  ScopeTest.

Q103


Given:

public class MainMethod {

       void main() {

             System.out.println("one");

       }

 

       static void main(String args) {

             System.out.println("two");

       }

 

       public static void main(String[] args) {

             System.out.println("three");

       }

 

       void mina(Object[] args) {

             System.out.println("four");

       }

}

 

What is printed out when the program is excuted?

A.
one

B.
two

C.
three

D.
four

La risposta è C.

Q102

Which two are valid array declaration?
A.
Object array[];
B.
Boolean array[3];
C.
int[] array;
D.
Float[2] array;

Risposta : A e C

Q101


class StaticField {

       static int i = 7;

 

       public static void main(String[] args) {

             StaticField obj = new StaticField();

             obj.i++;

             StaticField.i++;

             obj.i++;

             System.out.println(StaticField.i + " " + obj.i);

       }

}

What is the result?

A.
10 10

B.
8 9

C.
9 8

D.
7 10

 Risposta : A
i è statico e quindi è condiviso tra tutte le istanze della classe.

Q100

Which statement will emoty the contents of a StringBuilder variable named sb?
A.
sb.deleteAll();
B.
sb.delete(0, sb.size());
C.
sb.delete(0, sb.length());
D.
sb.removeAll();


Risposta B:
Risposta C:

             StringBuilder sb=new StringBuilder();

             sb.append("pippo");

             sb.delete(0, sb.length());

             System.out.println(sb.toString());

Q99


             boolean log3 = ( 5.0 != 6.0) && ( 4 != 5);

             boolean log4 = (4 != 4) || (4 == 4);

             System.out.println("log3:"+ log3 + "\nlog4:" + log4);

What is the result?

A.
log3:false
log4:true

B.
log3:true
log4:true

C.
log3:true
log4:false

D.
log3:false
log4:false

 Risposta: B

Q98


       int i, j=0;

       i = (3* 2 +4 +5 ) ;

       j = (3 * ((2+4) + 5));

       System.out.println("i:"+ i + "\nj:"+j);

What is the result?

A.
i: 16
j: 33

B.
i: 15
j: 33

C.
i: 33
j: 23

D.
i: 15
j: 23

 Risposta B

Q97


what should keyword1 and keyword2 be respectively, in oreder to produce output 2345?

int [] array = {1,2,3,4,5};

for (int i: array) {

if ( i < 2) {

keyword1 ;

}

System.out.println(i);

if ( i == 3) {

keyword2 ;

}

}

A.
continue, break

B.
break, break

C.
break, continue

D.
continue, continue

Risposta D :

             int[] array = { 1, 2, 3, 4, 5 };

             for (int i : array) {

                    if (i < 2) {

                          continue;// keyword1 ;

                    }

                    System.out.println(i);

                    if (i == 3) {

                          continue;// keyword2 ;

                    }

             } //2345

Q96


View the exhibit:

public class Student {

       public String name = "";

       public int age = 0;

       public String major = "Undeclared";

       public boolean fulltime = true;

       public void display() {

             System.out.println("Name: " + name + " Major: " + major);

       }

       public boolean isFullTime() {

             return fulltime;

       }

}

Which line of code initializes a student instance?

A.
Student student1;

B.
Student student1 = Student.new();

C.
Student student1 = new Student();

D.
Student student1 = Student();

 
Risposta C

venerdì 29 marzo 2013

Q95


Given

public class Main {

       public static void main(String[] args) {

             try {

                    doSomething();

             } catch (SpecialException e) {

                    System.out.println(e);

             }

       }

 

       static void doSomething() {

             int[] ages = new int[4];

             ages[4] = 17;

             doSomethingElse();

       }

 

       static void doSomethingElse() {

             throw new SpecialException("Thrown at end of doSomething() method");

       }

}

 

What is the output?

A.
SpecialException: Thrown at end of doSomething() method

B.
Error in thread “main” java.lang.ArrayIndexOutOfBoundseror

C.
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4 at Main.doSomething(Main.java:12)
at Main.main(Main.java:4)

D.
SpecialException: Thrown at end of doSomething() method at Main.doSomethingElse(Main.java:16)
at Main.doSomething(Main.java:13)
at Main.main(Main.java:4)

Risposta : questa domanda non è Chiara. Per compilar eil codice va cambiato in questo modo :

public class Main {

       public static void main(String[] args) throws SpecialException {

             doSomething();

       }

 

       static void doSomething() throws SpecialException {

             int[] ages = new int[4];

             ages[4] = 17;

             doSomethingElse();

       }

 

       static void doSomethingElse() throws SpecialException {

             throw new SpecialException("Thrown at end of doSomething() method");

       }

}

In questo caso si ottiene :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

       at Main.doSomething(Main.java:8)

       at Main.main(Main.java:3)