Welcome to your Test - 13 Code - 819

Name
Email
Phone
1.

Question:

Given:

public class ItemProcessor implements Runnable{  //LINE 1
    CyclicBarrier cb;
    public ItemProcessor(CyclicBarrier cb){
        this.cb = cb;
    }
    public void run(){
       System.out.println("processed");
        try {
            cb.await();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

public class Merger implements Runnable{ //LINE 2
    public void run(){
        System.out.println("Value Merged");
    }
}

What should be inserted in the following code such that run method of Merger will be executed only after the thread started at 4 and the main thread have both invoked await?

    public static void main(String[] args) throws Exception{
        var m = new Merger();

        //LINE 3

        var ip = new ItemProcessor(cb);
        ip.start(); //LINE 4
        cb.await();
    }

Select 1 option(s):
2.

Question:

Consider the code shown below:

public class TestClass{
  public static int switchTest(int k){
     var j = 1;
     switch(k){
        case 1: j++;
        case 2: j++;
        case 3: j++;
        case 4: j++;
        case 5: j++;
        default : j++;
     }
     return j + k;
  }
  public static void main(String[] args){
     System.out.println( switchTest(4) );
  }
}

What will it print when compiled and run?

Select 1 option(s):
3.

Question: What will the following code print when compiled and run?

import java.util.*;
public class TestClass {
    public static void main(String[] args) throws Exception {
        List list = new ArrayList();
        list.add("val1"); //1
        list.add(2, "val2"); //2
        list.add(1, "val3"); //3
        System.out.println(list);
     }
}


Select 1 option(s):
4.

Question:

Given the following class, which statements can be inserted at line 1 without causing the code to fail compilation?

public class TestClass{
int a;
int b = 0;
static int c;
public void m(){
int d;
int e = 0;
// Line 1
}
}

Select 4 option(s):

5.

Question: Which of the following are correct ways to initialize the static variables MAX and CLASS_GUID ?

class Widget{
static int MAX; //1
static final String CLASS_GUID; // 2
Widget(){
//3
}
Widget(int k){
//4
}
}

Select 2 option(s):

6.

Question: What can be added to the following Person class so that it is properly encapsulated and the code prints 29?

class Person{
    //Insert code here
}
public class Employee extends Person{
        public static void main(String[] args) {
            Employee e = new Employee();
            e.setAge(29);
            System.out.println(e.getAge());
        }
}


Select 2 option(s):
7.

Question:

Given that a method named Double getPrice(String id) exists and may potentially return null, about which of the following options can you be certain that a run time exception will not be thrown?

Select 2 option(s):
8.

Question:

Given:

class Square {
    private double side = 0;
    String color;
    public Square(double length){
        this.side = length;
    }
    public double getSide() {  return side;    }

    public void setSide(double side) {  this.side = side;   }
    
}

public class TestClass {
    public static void main(String[] args) throws Exception {
        Square mysq = new Square(10);
        mysq.color = "red";
        
        //set mysq's side to 20
    }
}


Which of the following statements will set the side of Square object referred by mysq to 20?

Select 1 option(s):
9.

Question:

Given the following class definition:

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

}
//1 : Insert code here


Which of the following would be a valid class that can be inserted at //1 ?

Select 2 option(s):
10.

Question:

Given:

public class Student {
    private String name;
    private int marks;
    public Student(String name, int marks){
        this.name = name;
        this.marks = marks;
    }
    public String toString(){
        return name+":"+marks;
    }
    //getters and setters not shown
}

What can be inserted in the code below so that it will print:
{20=[S1:20, S3:20], 30=[S3:30]}


List<Student> ls = Arrays.asList(new Student("S1", 20), new Student("S3", 30), new Student("S3", 20) );
//INSERT CODE HERE
System.out.println(grouping);


Select 1 option(s):
11.

Question:

Note: jmod is not on the part 1 exam. However some candidates have seen it mentioned in one of the incorrect options. You may read about jmod if you have time.

Which of the following options are supported by jmod?

Select 4 option(s):
12.

Question:

Consider the following code appearing in the same file:

class Data {
int x = 0, y = 0;
public Data(int x, int y){
this.x = x; this.y = y;
}
}
public class TestClass {
public static void main(String[] args) throws Exception {
Data d = new Data(1, 1);
//add code here
}
}

Which of the following options when applied individually will change the Data object currently referred to by the variable d to contain 2, 2 as values for its data fields?

Select 2 option(s):
13.

Question: What will the code shown below print when run?

class Wrapper{
int w = 10;
}
public class TestClass{
static Wrapper changeWrapper(Wrapper w){
w = new Wrapper();
w.w += 9;
return w;
}
public static void main(String[] args){
var w = new Wrapper();
w.w = 20;
changeWrapper(w);
w.w += 30;
System.out.println(w.w);
w = changeWrapper(w);
System.out.println(w.w);
}
}

Select 2 option(s):

14.

Question: What will the following program print when run?

public class Operators{

    public static int operators(){
        int x1 = -4;
        int x2 = x1--;
        int x3 = ++x2;
        if(x2 > x3){
            --x3;
        }else{
            x1++;
        }
        return x1 + x2 + x3;
    }
    public static void main(String[] args) {
        System.out.println(operators());
    }
}

Select 1 option(s):
15.

Question:

Given:

public class ThreadSafety{
List sList = new ArrayList();
public Student getStudent(){
var s = sList.remove(0);
return s;
}
...other irrelevant code
}

If two threads, T1 and T2, concurrently execute getStudent() method on the same ThreadSafety instance, which of the following are possible outcomes? (Assume that there are no nulls in the list.)

Select 3 option(s):
16.

Question:

Consider the following code:

import java.util.*;
import java.text.*;

public class TestClass
{
    public static void main(String[] args) throws Exception
    {
        double amount = 53000.35;
        
        Locale jp = new Locale("jp", "JP");
        
        //1 create formatter here.
        
        System.out.println(   formatter.format(amount)  );
    }
}


How will you create formatter using a factory at //1 so that the output is in Japanese Currency format?

Select 2 option(s):
17.

Question:

A programmer has written the following code for Address class. The programmer intends this class to be immutable.

public class Address {
    private String hNo;
    private String street;
    private String zip;
    public Address(String h, String s, String z){
        this.hNo = h; this.street = s; this.zip = z;
    }
    
    //only getters for three fields. No setters.
}

Which of the following actions implement the Java SE Secure guidelines?

Select 2 option(s):
18.

Question: What will the following code print?

List<Integer> str = Arrays.asList(1,2, 3, 4 );
str.stream().filter(x->{
    System.out.print(x+" ");
    return x>2;
});

Select 1 option(s):
19.

Question: What will the following command show?

jdeps --list-deps moduleA.jar

Select 2 option(s):
20.

Question:

Given:

@Target(value={TYPE_USE,TYPE_PARAMETER})
public @interface Interned{
}

and
public class Account{
}


Identify correct usages.

Select 2 option(s):
21.

Question:

Given that the file test.txt is accessible and contains multiple lines, which of the following code fragments will correctly print all the lines from the file?

Select 2 option(s):
22.

Question: What will the following code snippet print when compiled and run?

byte starting = 3;
short firstValue = 5;
int secondValue = 7;
int functionValue = (int) (starting/2 + firstValue/2 + (int) firstValue/3 ) + secondValue/2;
System.out.println(functionValue);

Select 1 option(s):
23.

Question: Which of the following statements about deadlock is correct?

Select 1 option(s):
24.

Question:

Given:

String INPUT_FILE = "c:\\temp\\src\\foo.bar\\module-info.java";

Assuming the file exists, which of the following options will print the contents of the file?

Select 2 option(s):
25.

Question:

Consider the following code:

interface Bar{
    void bar();
}

abstract class FooBase{
   
  public static void bar(){
     System.out.println("In static bar");  
  }    
}

public class Foo extends FooBase implements Bar {

}

What can be done to the above code so that it will compile without any error?

Select 1 option(s):
26.

Question:

Identify correct statement(s) about the following code:

        var value = 1,000,000; //1
        switch(value){
            case 1_000_000 : System.out.println("A million 1"); //2
                break;
            case 1000000 : System.out.println("A million 2"); //3
                break;
        }

Select 1 option(s):
27.

Question:

Given:

import java.io.FileNotFoundException;
import java.io.IOException;

public class Scrap {
    public static void main(String[] args) {
        try{
            if(args.length == 0) m2(); else m3();
        }
        
        INSERT CODE HERE

    }

    public static void m2() throws IOException { throw new FileNotFoundException(); }

    public static void m3() throws IndexOutOfBoundsException{ throw new IndexOutOfBoundsException(); }
}


Assuming that the above code is always invoked with at least one argument, what can be inserted in the above code to make it compile?

Select 2 option(s):
28.

Question:

You are writing a piece of code that determines tax rate on a given grossIncome. The tax rate is to be computed as follows -

   If grossIncome is less than or equals to 18000, taxRate is 0.
   If grossIncome is more than 18000 but less than or equal to 36000, taxRate is 10%
   If grossIncome is more than 36000, taxRate is 20%.

Which of following code fragments do it correctly?

Select 3 option(s):
29.

Question:

Consider the following method...

public static void ifTest(boolean flag){
if (flag) //1
if (flag) //2
if (flag) //3
System.out.println("False True");
else //4
System.out.println("True False");
else //5
System.out.println("True True");
else //6
System.out.println("False False");
}

Which of the following statements are correct ?

Select 2 option(s):
30.

Question: Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected, assuming no compiler optimizations are done?

public class NewClass{
private Object o;
void doSomething(Object s){ o = s; }
public static void main(String args[]){
Object obj = new Object(); // 1
NewClass tc = new NewClass(); //2
tc.doSomething(obj); //3
obj = new Object(); //4
obj = null; //5
tc.doSomething(obj); //6
}
}

Select 1 option(s):

31.

Question:

Given:

public class TestClass{

    static int someMethod(){ return 100; }

    static class MyWorker{
        Runnable r;
        public MyWorker(Runnable r){
            this.r = r;
        }
    }
    public static void main(String[] args) {
        //INSERT CODE HERE
        MyWorker w = new MyWorker(r);
    }
}


What can be inserted in the above code?

Select 3 option(s):
32.

Question:

Consider the following code:

class Super { static String ID = "QBANK"; }
class Sub extends Super{
static { System.out.print("In Sub"); }
}
public class Test{
public static void main(String[] args){
System.out.println(Sub.ID);
}
}

What will be the output when class Test is run?

Select 1 option(s):
33.

Question:

Given:

class M {
   static Object obj = null;
   public M(String val){ obj = val; }
}

class N{

   private M m = new M("hello");

   public static void main(String[] args){
      N n = new N();
      n = null;
      System.gc();
      System.out.println(M.obj);
   }
}


Assuming that System.gc() does its job, what will be printed?

Select 1 option(s):
34.

Question:

Given:

class MyProcessor{
    public void process(){
        System.out.println("Processing ");
    }
}
public class TestClass {
    
    public static void main(String[] args) {
        process(()->new MyProcessor()); //1 REPLACE THIS LINE OF CODE
    }

    public static void process(Supplier<MyProcessor> s){
        s.get().process();
    }
}

Which of the following options correctly replaces the line marked //1 with code that uses method reference?

Select 1 option(s):
35.

Question:

Consider the following class :

public class Test{
   public static void main(String[] args){
      if (args[0].equals("open"))
         if (args[1].equals("someone"))
            System.out.println("Hello!");
      else System.out.println("Go away "+ args[1]);
    }
}


Which of the following statements are true if the above program is run with the command line :

Select 1 option(s):
36.

Question:

Given:

String qr = INSERT CODE HERE
try(PreparedStatement ps =  connection.prepareStatement(qr);){

...

}

What can be inserted in the above code?

Select 1 option(s):
37.

Question:

Given the following code fragment, which of the following lines would be a part of the output?

outer:
for ( var i = 0 ; i<3 ; i++ ){
for ( var j = 0 ; j<2 ; j++ ){
if ( i == j ){
continue outer;
}
System.out.println( "i=" + i + " , j=" + j );
}
}

Select 2 option(s):

38.

Question:

Considering the following program, which of the options are true?

public class FinallyTest{
   public static void main(String args[]){
      try{
          if (args.length == 0) return;
          else throw new Exception("Some Exception");
      }
      catch(Exception e){
          System.out.println("Exception in Main");
      }
      finally{
          System.out.println("The end");
      }
   }
}

Select 2 option(s):
39.

Question:

Given:


import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
public class Cache {
    
    static ConcurrentHashMap<String, Object> chm = new ConcurrentHashMap<String, Object>();
    
    public static void main(String[] args) {
        chm.put("a", "aaa");
        chm.put("b", "bbb");
        chm.put("c", "ccc");
        
        new Thread(){
            public void run(){
                Iterator<Entry<String, Object>> it = Cache.chm.entrySet().iterator();
                while(it.hasNext()){
                    Entry<String, Object> en = it.next();
                    if(en.getKey().equals("a") || en.getKey().equals("b")){
                        it.remove();
                    }
                }
            }
        }.start();
        
        new Thread(){
            public void run(){
                Iterator<Entry<String, Object>> it = Cache.chm.entrySet().iterator();
                while(it.hasNext()){
                    Entry<String, Object> en = it.next();
                    System.out.print(en.getKey()+", ");
                }
            }
        }.start();        
    }
}


Which of the following are possible outputs when the above program is run?

Select 1 option(s):
40.

Question: What will the following code print?

import java.util.Optional;
public class NewClass {
    public static Optional<String> getGrade(int marks){
        Optional<String> grade = Optional.empty();
        if(marks>50){
            grade = Optional.of("PASS");
        }
        else {
            grade.of("FAIL");
        }
        return grade;
    }
    public static void main(String[] args) {
        Optional<String> grade1 = getGrade(50);
        Optional<String> grade2 = getGrade(55);
        System.out.println(grade1.orElse("UNKNOWN"));
        if(grade2.isPresent()){
            grade2.ifPresent(x->System.out.println(x));
        }else{
            System.out.println(grade2.orElse("Empty"));
        }
    }
}

Select 1 option(s):
41.

Question: Which statements about the following code are correct?

interface House{
  public default String getAddress(){
     return "101 Main Str";
  }
}

interface Office {
  public default String getAddress(){
     return "101 Smart Str";
  }
}

class HomeOffice implements House, Office{
  public String getAddress(){
     return "R No 1, Home";
  }
}

public class TestClass {

  public static void main(String[] args) {
    House h = new HomeOffice();  //1
    System.out.println(h.getAddress()); //2
  }
}

Select 1 option(s):
42.

Question:

Consider the following code:

class Base{
private float f = 1.0f;
void setF(float f1){ this.f = f1; }
}
class Base2 extends Base{
private float f = 2.0f;
//1
}

Which of the following options is/are valid example(s) of overriding?

Select 2 option(s):
43.

Question: Which of the following commands can be used to identify class and module dependencies of a class named test.A of module named moduleA without executing it?

Assume that all module files are stored in out directory.

Note: Although identifying module dependency is not explicitly mentioned in the exam objectives, we have seen questions on the exam that require you to know about the tool that is used to identify module dependencies.

Select 1 option(s):
44.

Question:

You are trying to compile your module named mycompany.finance. You have put all the source files for your module in src directory. However, your module requires a mycompany.utils module, which is delivered by another team of your company in the form of mycompany.utils.jar file. You have kept this jar in libs directory.

Which of the following commands can be used to compile your module?

Select 1 option(s):
45.

Question: What will be the result when you try to compile and run the following code?

public class TestClass
{
   public static void main(String args[] )
   {
      var out = new Outer();
      System.out.println(out.getInner().getOi());
   }
}
class Outer
{
   private int oi = 20;
   class Inner
   {
      int getOi() { return oi; }
   }
   Inner getInner() { return new Inner() ; }
}

Select 1 option(s):
46.

Question: What will be the output when the following program is run?

public class TestClass{
char c;
public void m1(){
char[ ] cA = { 'a' , 'b'};
m2(c, cA);
System.out.println( ( (int)c) + "," + cA[1] );
}
public void m2(char c, char[ ] cA){
c = 'b'; cA[1] = cA[0] = 'm';
}
public static void main(String args[]){
new TestClass().m1();
}
}

Select 1 option(s):

47.

Question: What will be the output of the following program?

class TestClass{
   public static void main(String[] args) throws Exception{
      try{
         amethod();
         System.out.println("try ");
      }
      catch(Exception e){
         System.out.print("catch ");
      }
      finally   {
         System.out.print("finally ");
      }
      System.out.print("out ");
   }
   public static void amethod(){ }
}

Select 1 option(s):
48.

Question:

Given:

String sentence = "Life is a box of chocolates, Forrest. You never know what you're gonna get."; //1
Optional<String> theword = Stream.of(sentence.split("[ ,.]")).anyMatch(w->w.startsWith("g")); //2
System.out.println(theword.get()); //3

Which of the following statements are correct?

Select 1 option(s):
49.

Question:

Consider the following method:

public void setSQLMode(Connection c, String mode) throws Exception{
    Statement stmt = c.createStatement();
    String qr = "SET SESSION sql_mode = '"+mode+"';";
    stmt.execute(qr);
}


Identify the correct statement about the above code.

Select 1 option(s):
50.

Question: What will the following code print?

   Path p1 = Paths.get("c:\\temp\\test.txt");
   Path p2 = Paths.get("c:\\temp\\report.pdf");
   System.out.println(p1.resolve(p2));

Select 1 option(s):
51.

Question:

Given:

enum Coffee
{
    ESPRESSO("Very Strong"), MOCHA("Bold"), LATTE("Mild");
    public String strength;
    Coffee(String strength)
    {
        this.strength = strength;
    }
    public String toString(){   return strength; }
}

Which of the given code snippets will produce the following output:

ESPRESSO:Very Strong, MOCHA:Bold, LATTE:Mild,

Select 1 option(s):
52.

Question: Which of the following lambda expressions can be used to implement a Function<Integer, String> ?

Select 1 option(s):
53.

Question:

Given the following code that appears inside a method:

   var values = new ArrayList<String>();
   //INSERT CODE HERE

What can be inserted at the given location without causing any compilation error?

Select 4 option(s):
54.

Question: Which of the changes given in options can be done (independent of each other) to let the following code compile and run without errors when its generateReport method is called?

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
   }
}

Select 2 option(s):