Java Test.
 

This is the first question. It only serves to make you feel confident with this quiz.
You have 3 minutes to answer each question including this.
Start when you're ready.
This is the correct answer A
This is the incorrect answer
Domanda
 
Given:
3.   public class RediMix extends Concrete {
4.     RediMix() { System.out.println("r "); }
5.     public static void main(String[] args) {
6.        new RediMix();
7.     }
8.   }
9.   class Concrete extends Sand {
10.    Concrete() { System.out.print("c "); }
11.    private Concrete(String s) { }
12.  }
13.  abstract class Sand {
14.    Sand() { System.out.print("s "); }
15.   }
 
What is the result?
R
C r
R c
S c r
R c s
Compilation fails due to a single error in the code.
Compilation fails due to multiple errors in the code.
Given:
 
2.  import java.text.*;
3.  public class Gazillion {
4.    public static void main(String[] args) throws Exception {
5.      String s = "123.456xyz";
6.      NumberFormat nf = NumberFormat.getInstance();
7.      System.out.println(nf.parse(s));
8.      nf.setMaximumFractionDigits(2);
9.      System.out.println(nf.format(s));
10.    }
11.  }
 
Which are true? (Choose all that apply.)
Compilation fails.
The output will contain "123.45 "
The output will contain "123.456"
The output will contain "123.456xyz"
An exception will be thrown at runtime.
Given:
 
3.  public class Dec26 {
4.    public static void main(String[] args) {
5.      short a1 = 6;
6.      new Dec26().go(a1);
7.      new Dec26().go(new Integer(7));
8.    }
9.    void go(Short x) { System.out.print("S "); }
10.   void go(Long x) { System.out.print("L "); }
11.   void go(int x) { System.out.print("i "); }
12.   void go(Number n) { System.out.print("N "); }
13.  }
 
What is the result?
I L
I N
S L
S N
Compilation fails.
An exception is thrown at runtime.
Given:

2.  public class Contact {
3.    private String name;
4.    private String city;
5.    String getName() { return name; }
6.    void setName(String n) { name = n; }
7.    void setCity(String c) {
8.      if(c == null) throw new NullPointerException();
9.      city = c;
10.   }
11.   String getCity() { return city; }
12.  }

Which are true? (Choose all that apply.)
Compilation fails.
The class is well encapsulated.
The setCity() method is an example of loose coupling.
The setCity() method has better encapsulation than setName().
The setCity() method is cohesive; the setName() method is not.
Given:

343.  String s = "1234";
344.  StringBuilder sb =
345.    new StringBuilder(s.substring(2).concat("56").replace("7","6"));
346.  System.out.println(sb.append("89").insert(3,"x"));

What is the result?
34x5689
345x689
345x789
23x45689
23x45789
Compilation fails.
Given the proper imports and given:

81.    String in = "1234,77777,689";
82.    Scanner sc = new Scanner(in);
83.    sc.useDelimiter(",");
84.    while(sc.hasNext())
85.       System.out.print(sc.nextInt() + " ");
86.    while(sc.hasNext())
87.       System.out.print(sc.nextShort() + " ");

What is the result?
1234 77777 689
Compilation fails.
1234 77777 689 1234 77777 689
1234 followed by an exception.
1234 77777 689 followed by an exception.
1234 77777 689 1234 followed by an exception.
Given:

2.  public class Errrrr {
3.    static String a = null;
4.    static String s = "";
5.    public static void main(String[] args) {
6.      try {
7.        a = args[0];
8.        System.out.print(a);
9.        s += "t1 ";
10.     }
11.     catch (RuntimeException re) { s += "c1 "; }
12.     finally { s += "f1 "; }
13.     System.out.println(" " + s);
14.  } }
 
And two command-line invocations:
java Errrrr
java Errrrr x
 
What is the result?
First: f1, then: x t1
First: f1, then: x t1 f1
First: c1, then: x t1
First: c1, then: x t1 f1
First: c1 f1, then: x t1
First: c1 f1, then: x t1 f1
Compilation fails.
Given the proper import(s), and given:

13.  class NameCompare implements Comparator<Stuff> {
14.    public int compare(Stuff a, Stuff b) {
15.      return b.name.compareTo(a.name);
16.  } }
18.  class ValueCompare implements Comparator<Stuff> {
19.    public int compare(Stuff a, Stuff b) {
20.      return (a.value - b.value);
21. } }

Which are true? (Choose all that apply.)
This code does not compile.
This code allows you to use instances of Stuff as keys in Maps.
These two classes properly implement the Comparator interface.
NameCompare allows you to sort a collection of Stuff instances alphabetically.
ValueCompare allows you to sort a collection of Stuff instances in ascending numeric order.
If you changed both occurrences of "compare()" to "compareTo()", the code would compile.
Given:

2.  class Horse {
3.    static String s = "";
4.    void beBrisk() { s += "trot "; }
5.  }
6.  public class Andi extends Horse {
7.    void beBrisk() { s += "tolt "; }
8.    public static void main(String[] args) {
9.      Horse x0 = new Horse();
10.     Horse x1 = new Andi();  x1.beBrisk();
11.     Andi x2 = (Andi)x1;     x2.beBrisk();
12.     Andi x3 = (Andi)x0;     x3.beBrisk();
13.     System.out.println(s);
14.  } }
 
What is the result?
Tolt tolt tolt
Trot tolt trot
Trot tolt tolt
Compilation fails.
An exception is thrown at runtime.
Given the following two files containing Light.java and Dark.java:

2. Package ec.ram;
3. Public class Light{}
4. class Burn{}
 
2. Package ec.ram;
3. Public class Dark{}
4. class Melt{}
 
And if those files are located in the following directory structure:
 
$ROOT
   |-- Light.java
   |-- Dark.java
   |-- checker
           |-- dira
           |-- dirb
 
And the following commands are executed, in order, from the ROOT directory:

javac Light.java -cp checker/dira -d checker/dirb
javac Dark.java -cp checker/dirb -d checker/dira
jar -cf checker/dira/a.jar checker
 
A new JAR file is created after executing the above commands. Which of the following files will exist inside that JAR file? (Choose all that apply.)
[JAR]/dira/ec/ram/Melt.class
[JAR]/checker/dirb/ec/ram/Burn.class
[JAR]/dirb/ec/ram/Melt.class
[JAR]/checker/dira/ec/ram/Burn.class
[JAR]/dira/a.jar
[JAR]/checker/dira/a.jar
Given the proper import(s), and this code in a method:

4.   List<String> x = new LinkedList<String>();
5.   Set<String> hs = new HashSet<String>();
6.   String[] v = {"a", "b", "c", "b", "a"};
7.   for(String s: v) {
8.     x.add(s); hs.add(s);
9.   }
10.  System.out.print(hs.size() + " " + x.size() + " ");
11.  HashSet hs2 = new HashSet(x);
12.  LinkedList x2 = new LinkedList(hs);
13.  System.out.println(hs2.size() + " " + x2.size());

What is the result?
3 3 3 3
3 5 3 3
3 5 3 5
5 5 3 3
5 5 5 5
Compilation fails.
An exception is thrown at runtime.
Given:
1. Import java.util.*;
2. Public class Elway {
3.   public static void main(String[] args) {
4.     ArrayList[] ls = new ArrayList[3];
5.     for(int I = 0; I < 3; i++) {
6.       ls[i] = new ArrayList();
7.       ls[i].add("a" + i);
8.     }
9.     Object o = ls;
10.    do3(ls);
11.    for(int I = 0; I < 3; i++) {
12.      // insert code here
13.    }
14.  }
15.  static Object do3(ArrayList[] a) {
16.    for(int I = 0; I < 3; i++) a[i].add("e");
17.    return a;
18. } }

And the following fragments:
I.   System.out.print(o[i] + " ");
II.  System.out.print((ArrayList[])[i] + " ");
III. System.out.print( ((Object[])o)[i] + " ");
IV.  System.out.print(((ArrayList[])o)[i] + " ");
 
If the fragments are added to line 12, independently, which are true? (Choose all that apply.)
Fragment I will compile.
Fragment II will compile.
Fragment III will compile.
Fragment IV will compile.
Compilation fails due to other errors.
Of those that compile, the output will be [a0] [a1] [a2]
Of those that compile, the output will be [a0, e] [a1, e] [a2, e]
Given this code in a method:

5.    boolean[] ba ={true, false};
6.    short[][] gr = {{1,2}, {3,4}};
7.    int I = 0;
8.    for( ; I < 10; ) i++;
9.    for(short s: gr) ;
10.   for(int j = 0, k = 10; k > j; ++j, k--) ;
11.   for(int j = 0; j < 3; System.out.println(j++)) ;
12.   for(Boolean b: ba) ;

What is the result? (Choose all that apply.)
Compilation succeeds.
Compilation fails due to an error on line 8.
Compilation fails due to an error on line 9.
Compilation fails due to an error on line 10.
Compilation fails due to an error on line 11.
Compilation fails due to an error on line 12.
Given:

2.  class Engine {
3.    public class Piston {
4.      static int count = 0;
5.      void go() { System.out.print(" pump " + ++count); }
6.    }
7.    public Piston getPiston() { return new Piston(); }
8.  }
9.  public class Auto {
10.   public static void main(String[] args) {
11.     Engine e = new Engine();
12.     // Engine.Piston p = e.getPiston();
13.     e.Piston p = e.getPiston();
14.     p.go(); p.go();
15. } }

In order for the code to compile and produce the output " pump 1 pump 2", which are true? (Choose all that apply.)
The code is correct as it stands.
Line 4 must be changed. Count can’t be declared "static"
Line 12 must be un-commented, and line 13 must be removed.
Somewhere in the code, a second instance of Piston must be instantiated.
There are errors in the code that must be fixed, outside of lines 4, 12, and 13.
Given:

1.  import java.util.*;
2.  public class Piles {
3.    public static void main(String[] args) {
4.      TreeMap<String, String> tm = new TreeMap<String, String>();
5.      TreeSet<String> ts = new TreeSet<String>();
6.      String[] k = {"1", "b", "4", "3"};
7.      String[] v = {"a", "d", "3", "b"};
8.      for(int i=0; i<4; i++) {
9.        tm.put(k[i], v[i]);
10.       ts.add(v[i]);
11.     }
12.     System.out.print(tm.values() + " ");
13.     Iterator it2 = ts.iterator();
14.     while(it2.hasNext()) System.out.print(it2.next() + "-");
15.  } }
 
Which of the following could be a part of the output? (Choose two.)
[a, b, 3, d]
[d, a, b, 3]
[3, a, b, d]
[a, b, d, 3]
[1, 3, 4, b]
[b, 1, 3, 4]
3-a-b-d-
A-b-d-3-
A-d-3-b-
Given:
3.  public class BigData {
4.    static BigData bd;
5.    public static void main(String[] args) {
6.      new BigData().doStuff();
7.      // do lots of memory intensive stuff
...     // JVM finds an eligible BigData object for GC
...     // JVM invokes finalize()
...     // do more stuff
48.   }
49.   void doStuff() {  }
50.   // insert code here
51.     bd = this;
52.   }
53.  }
54.  class MyException extends Exception {  }

and the following four fragments:

I.   protected void finalize() throws Throwable {
II.  protected void finalize() {
III. Protected void finalize() throws MyException {
IV.  void finalize() {
 
If the fragments are inserted, independently, at line 50, which are true? (Choose all that apply.)
Fragment I compiles.
Fragment II compiles.
Fragment III compiles.
Fragment IV compiles.
Of those that compile, the GC will collect any given object after the JVM has called finalize() on that object.
Because of the way finalize() has been overridden, the GC will never collect eligible objects of type BigData.
Given this code in a method:
 
3.    int y, count = 0;
4.    for(int x = 3; x < 6; x++) {
5.      try {
6.        switch(x) {
7.          case 3: count++;
8.          case 4: count++;
9.          case 7: count++;
10.         case 9: { y = 7 / (x - 4); count += 10; }
11.       }
12.     } catch (Exception ex) { count++; }
13.   }
14.   System.out.println(count);
 
What is the result?
2
15
16
25
26
Compilation fails.
An exception is thrown with no other output.
Given:

4.  public class Hemlock {
5.    static StringBuffer sb;
6.    StringBuffer sb2;
7.    public static void main(String[] args) {
8.      sb = sb.append(new Hemlock().go(new StringBuffer("hey")));
9.      System.out.println(sb);
10.   }
11.   { sb2 = new StringBuffer("hi "); }
12.   StringBuffer go(StringBuffer s) {
13.     System.out.print(s + " oh " + sb2);
14.     return new StringBuffer("ey");
15.   }
16.   static { sb = new StringBuffer("yo "); }
17. }

What is the result?
Yo ey
Hey oh hi
Hey oh hi ey
Oh hi hey
Hey oh hi yo ey
Yo hey oh hi ey
Compilation fails.
An exception is thrown at runtime.
Given:

2.  import java.util.*;
3.  public class GIS {
4.    public static void main(String[] args) {
5.      TreeMap<String, String> m1 = new TreeMap<String, String>();
6.      m1.put("a", "amy"); m1.put("f", "frank");
7.      NavigableMap<String, String> m2 = m1.descendingMap();
8.      try {
9.        m1.put("j", "john");
10.       m2.put("m", "mary");
11.     }
12.     catch (Exception e) { System.out.print("ex "); }
13.     m1.pollFirstEntry();
14.     System.out.println(m1 + "\n" + m2);
15.  } }
 
What is the result?
{f=frank, j=john} {f=frank}
{f=frank, j=john} {m=mary, f=frank}
Ex {f=frank, j=john} {f=frank}
{f=frank, j=john, m=mary} {m=mary, j=john, f=frank}
Ex {f=frank, j=john, m=mary} {f=frank}
Ex {f=frank, j=john, m=mary} {f=frank, a=amy}
{a=amy, f=frank, j=john, m=mary} {f=frank, a=amy}
Compilation fails due to error(s) in the code.
Given:

2.   public class Clover extends Harrier {
3.     String bark() { return "feed me "; }
4.     public static void main(String[] args) {
5.       Dog[] dogs = new Dog[3];
6.       dogs[0] = new Harrier();
7.       dogs[1] = (Dog)new Clover();
8.       dogs[2] = (Dog)new Harrier();
9.       for(Dog d: dogs) System.out.print(d.bark());
10.  } }
11.  class Dog { String bark() { return "bark "; } }
12.  class Harrier extends Dog { String bark() { return "woof "; } }
 
What is the result? (Choose all that apply.)
Bark bark bark
Woof bark bark
Woof feed me woof
Compilation fails due to an error on line 6.
Compilation fails due to an error on line 7.
Compilation fails due to an error on line 8.
Compilation fails due to an error on line 9.
{"name":"Java Test.", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"Questa è la prima domanda. Serve solo per farti prendere confidenza con questo quiz.   Hai 3 minuti per dare la risposta ad ogni domanda compresa questa.   Inizia quando sei pronto., Domanda   Given:3.   public class RediMix extends Concrete { 4.     RediMix() { System.out.println(\"r \"); } 5.     public static void main(String[] args) { 6.        new RediMix(); 7.     } 8.   } 9.   class Concrete extends Sand { 10.    Concrete() { System.out.print(\"c \"); } 11.    private Concrete(String s) 12.  } 13.  abstract class Sand { 14.    Sand() { System.out.print(\"s \"); } 15.   }   What is the result?, Given:   2.  import java.text.*; 3.  public class Gazillion { 4.    public static void main(String[] args) throws Exception { 5.      String s = \"123.456xyz\"; 6.      NumberFormat nf = NumberFormat.getInstance(); 7.      System.out.println(nf.parse(s)); 8.      nf.setMaximumFractionDigits(2); 9.      System.out.println(nf.format(s)); 10.    } 11.  }   Which are true? (Choose all that apply.)","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}
Powered by: Quiz Maker