Welcome to your Test - 12 Code - 819

Name
Email
Phone
1.

Question: What will the following program print?

class LoopTest{
public static void main(String args[]) {
var counter = 0;
outer:
for (var i = 0; i < 3; i++) {
middle:
for (var j = 0; j < 3; j++) {
inner:
for (var k = 0; k < 3; k++) {
if (k - j > 0) {
break middle; }
counter++;
}
}
}
System.out.println(counter);
}
}

Select 1 option(s):

2.

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

public class Device implements AutoCloseable{
    boolean open = false;
    int index;
    public Device(int index){
        this.index = index;
        open = true;
    }
    
    public void write() throws IOException{
        throw new RuntimeException("Can't write!");
    }
    
    public void close(){
        open = false;
        System.out.println("Device closed "+index);
    }
    
    public static void main(String[] args) {
        Device d1 = new Device(1);
        try(d1;
            Device d2 = new Device(2);
            Device d3 = new Device(3)){
            d2.write();
            d1.close();
        }catch(Exception e){
            System.out.println("Got Exception "+e.getMessage());
        }
    }
}

Select 1 option(s):
3.

Question: Which of the following are valid approaches for input validation from security perspective?

Select 2 option(s):
4.

Question: How many times will the line marked //1 be called in the following code?

var x = 10;
do{
x--;
System.out.println(x); // 1
}while(x<10);

Select 1 option(s):

5.

Question:

Identify correct statements above the following code:

   var myES = Executors.newSingleThreadExecutor();
   myES.submit(() -> {}); //1
   myES.submit(() -> 100);//2


Select 2 option(s):
6.

Question:

Given:

String qr = "insert into STOCK ( ID, TICKER, LTP, EXCHANGE ) values( ?, ?, ?, ?)";
String[] tickers = {"AA", "BB", "CC", "DD" };

You are trying to initialize the STOCK table and for that you need to insert one row for each of the ticker value in the tickers array. Each row has to be initialized with the same values except the ID and TICKER columns, which are different for each row. The ID column is defined as AUTO_INCREMENT and so you need to pass only 0 for this column.

Which of the following code snippets would you use?

Select 1 option(s):
7.

Question:

Given:

public static void main(String[] args) {
    Device d1 = new Device(1);
    try(d1){
       //do some thing with d1
    }
}


Identify correct statement(s).

Select 1 option(s):
8.

Question: Which digits and in what order will be printed when the following program is run?

public class TestClass{
public static void main(String args[]){
int k = 0;
try{ int i = 5/k;
}
catch (ArithmeticException e){
System.out.println("1");
}
catch (RuntimeException e){
System.out.println("2");
return ;
}
catch (Exception e){
System.out.println("3");
}
finally{
System.out.println("4");
}
System.out.println("5");
}
}

Select 1 option(s):

9.

Question: What can be inserted in the following code so that it will print [21, 32, 43] ?

List<Integer> ls = Arrays.asList(11, 22, 33);
//INSERT CODE HERE
ls.replaceAll(func);
System.out.println(ls);

Select 1 option(s):
10.

Question: Which of the following commands can be used to identify class and module dependencies?

Select 1 option(s):
11.

Question:

You have been given an array of objects and you need to process this array as follows -

1. Call a method on each object from first to last one by one.
2. Call a method on each object from last to first one by one.
3. Call a method on only those objects at even index (0, 2, 4, 6, etc.)

Which of the following are correct?

Select 1 option(s):
12.

Question:

Consider the following code:

class Outsider
{
   public class Insider{ }
}
public class TestClass
{
   public static void main(String[] args)
   {
       var os = new Outsider();
       // 1 insert line here
    }
}


Which of the following options can be inserted at //1?

Select 1 option(s):
13.

Question:

Consider the following code:

import java.util.ArrayList;
public class Student{
ArrayList scores;
private double average;
public ArrayList getScores(){ return scores; }
public double getAverage(){ return average; }
private void computeAverage(){
//valid code to compute average
average =//update average value
}
public Student(){
computeAverage();
}
//other code irrelavant to this question not shown
}

What can be done to improve the encapsulation of this class?

Select 2 option(s):
14.

Question: What will be the result of attempting to compile and run the following program?

public class TestClass{
   public static void main(String args[ ] ){
      String s = "hello";
      StringBuilder sb = new StringBuilder( "hello" );
      sb.reverse();
      s.reverse();
      if( s == sb.toString() )  System.out.println( "Equal" );
      else System.out.println( "Not Equal" );
   }
}

Select 1 option(s):
15.

Question: What will the following program print?

public class InitTest{
public InitTest(){
s1 = sM1("1");
}
static String s1 = sM1("a");
String s3 = sM1("2");{
s1 = sM1("3");
}
static{
s1 = sM1("b");
}
static String s2 = sM1("c");
String s4 = sM1("4");
public static void main(String args[]){
InitTest it = new InitTest();
}
private static String sM1(String s){
System.out.println(s); return s;
}
}

Select 1 option(s):

16.

Question: What will be the result of attempting to compile and run the following class?

public class InitTest{
static String s1 = sM1("a");{
s1 = sM1("b");
}
static{
s1 = sM1("c");
}
public static void main(String args[]){
InitTest it = new InitTest();
}
private static String sM1(String s){
System.out.println(s); return s;
}
}

Select 1 option(s):

17.

Question:

Given:

StringBuilder sb = new StringBuilder("abcdef");
//INSERT CODE HERE
for(int i = 0, k = sb.length(); i<k; i++){
    sb.replace(i, i+1, f.apply(sb.charAt(i)));
}
System.out.println(sb);

Which of the following statements can be inserted in the above code?

Select 1 option(s):
18.

Question:

Consider the following code:

class Test{
  public static void main(String[] args){
    for (int i = 0; i < args.length; i++)   System.out.print(i == 0 ? args[i] : " " + args[i]);
  }
}


What will be the output when it is run using the following command:

java Test good bye friend!

Select 1 option(s):
19.

Question:

How can you declare 'i' so that it is not visible outside the package test.

package test;
public class Test{
XXX int i;
/* irrelevant code */
}

Select 2 option(s):

20.

Question:

Given that a class named com.xyz.fx.Main is part of a module named xyz.fx packaged in fx.jar, which of the following commands can be used to execute this class?

(Assume that the jar file is located in the current directory.)

Select 2 option(s):
21.

Question:

You want to execute a task that returns a result without blocking. Which of the following types from java.util.concurrent package will be required to achieve this?

Select 4 option(s):
22.

Question:

Consider the following class and interface definitions (in separate files):

public class Sample implements IInt{
   public static void main(String[] args){
      Sample s = new Sample();  //1
      int j = s.thevalue;       //2
      int k = IInt.thevalue;    //3
      int l = thevalue;         //4
   }
}
public interface IInt{
      int thevalue = 0;
}


What will happen when the above code is compiled and run?

Select 1 option(s):
23.

Question:

Given:

interface Account{
  public default String getId(){
     return "0000";
  }
}

interface PremiumAccount extends Account{
   //INSERT CODE HERE
}

Which of the following options can be inserted in PremiumAccount independent of each other?

Select 2 option(s):
24.

Question:

Given:

public static void reader(String fileName1) throws Exception{
    try (var fr = new FileReader(fileName1);) {
        int charRead = 0;
        while ((charRead = fr.read()) != -1) {
            System.out.println("Read char " + charRead);
        }
    }
}


What can be done to the above code to make it read Strings instead of chars?

Select 1 option(s):
25.

Question: Which of the following lambda expressions can be used to invoke a method that accepts a java.util.function.Predicate as an argument?

Select 2 option(s):
26.

Question:

Consider the following code:

        var s = "hello";
        byte i = 100;
        var fos = new FileOutputStream("c:\\temp\\data.bin");
        var dos = new DataOutputStream(fos);
        //WRITE s to file
        //WRITE i to file
        dos.flush(); dos.close(); fos.close();

        var dis = new DataInputStream(new FileInputStream("c:\\temp\\data.bin"));
        //READ s from file
        //READ i from file


Which methods should be used to write and read s and i to/from the data.bin file?

Select 1 option(s):
27.

Question:

Given:

@Retention(RetentionPolicy.RUNTIME)
public @interface DebugInfo {
    String value() default "";
    String[] params();
    String date();
    int depth() default 10;
}

Which of the following options correctly uses the above annotation?

Select 2 option(s):
28.

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

abstract class Calculator{
   abstract void calculate();
   public static void main(String[] args){
      System.out.println("calculating");
      Calculator x = null;
      x.calculate();
   }
}

Select 1 option(s):
29.

Question:

Consider the following code:

import java.util.*;
public class TestClass {
    public static void main(String[] args)
    {
      // put declaration here

      m.put("1", new ArrayList());    //1
      m.put(1, new Object());    //2
      m.put(1.0, "Hello");     //3
      System.out.println(m);
    }
}


How can 'm' be declared such that the above code will compile and run without errors?

Select 2 option(s):
30.

Question: What will the following code print?

   Path p1 = Paths.get("\\photos\\vacation");
   Path p2 = Paths.get("\\yellowstone");
   System.out.println(p1.resolve(p2)+"  "+p1.relativize(p2));

Select 1 option(s):
31.

Question: Which of the following classes have a default constructor?

class A{  }
class B {  B(){ } }
class C{  C(String s){ } }

Select 1 option(s):
32.

Question: Which of the following are valid method implementations?

Select 2 option(s):
33.

Question:

Given:

class Person{
    String name;
    String dob;
    public Person(String name, String dob){
        this.name = name; this.dob = dob;
    }
}
class MySorter {
    public int compare(Person p1, Person p2){
        return p1.dob.compareTo(p2.dob);
    }
}
public class SortTest {
    public static int diff(Person p1, Person p2){
        return p1.dob.compareTo(p2.dob);
    }
    
    public static int diff(Date d1, Date d2){
        return d1.compareTo(d2);
    }
    public static void main(String[] args) {
        ArrayList<Person> al = new ArrayList<>();
        al.add(new Person("Paul", "01012000"));
        al.add(new Person("Peter", "01011990"));
        al.add(new Person("Patrick", "01012002"));        
        
         INSERT CODE HERE
    }
}

and the following lines of code:
I     java.util.Collections.sort(al, (p1, p2)->p1.dob.compareTo(p2.dob));
        
II    java.util.Collections.sort(al, SortTest::diff);

III   java.util.Collections.sort(al, new MySorter()::compare);
        
IV    java.util.Arrays.sort(al, SortTest::diff);

How many of the above lines can be inserted into the given code, independent of each other, to sort the list referred to by al?

Select 1 option(s):
34.

Question:

Identify correct statements about following command:

java -p .\dir1 -cp .\dir2 -m a.b/a.b.c.Main

Select 1 option(s):
35.

Question:

Consider the following piece of code, which is run in an environment where the default locale is English - US:

        Locale.setDefault(new Locale("fr", "CA")); //Set default to French Canada
        Locale l = new Locale("jp", "JP");
        ResourceBundle rb = ResourceBundle.getBundle("appmessages", l);
        String msg = rb.getString("greetings");
        System.out.println(msg);

You have created two resource bundles for appmessages, with the following contents:

#In English US resource bundle file
greetings=Hello

#In French CA resource bundle file
greetings=bonjour


What will be the output?

Select 1 option(s):
36.

Question:

After which line will the object created at line XXX be eligible for garbage collection?

public Object getObject(Object a) //0
   {

Object b = new Object();  //XXX

Object c, d = new Object(); //1
c = b; //2
b = a = null; //3
return c; //4
}

Select 1 option(s):
37.

Question:

Identify correct statements about the module system of Java.

Select 2 option(s):
38.

Question:

Given:

class A{
  public List<Number> getList(){
      //valid code
  };
}
class B extends A{
  @Override
  *INSERT CODE HERE*
      //valid code
  };
}

What can be inserted in the above code?

Select 1 option(s):
39.

Question:

Consider that you are writing a set of classes related to a new Data Transmission Protocol and have created your own exception hierarchy derived from java.lang.Exception as follows:

enthu.trans.ChannelException
+-- enthu.trans.DataFloodingException,
enthu.trans.FrameCollisionException

You have a TransSocket class that has the following method:

long connect(String ipAddr) throws ChannelException

Now, you also want to write another "AdvancedTransSocket" class, derived from "TransSocket" which overrides the above mentioned method. Which of the following are valid declaration of the overriding method?

Select 2 option(s):
40.

Question:

Consider the following class:

class TestClass{
void probe(int... x) { System.out.println("In ..."); } //1
void probe(Integer x) { System.out.println("In Integer"); } //2
void probe(long x) { System.out.println("In long"); } //3
void probe(Long x) { System.out.println("In LONG"); } //4
public static void main(String[] args){
Integer a = 4; new TestClass().probe(a); //5
int b = 4; new TestClass().probe(b); //6
}
}

What will it print when compiled and run?

Select 2 option(s):
41.

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

public class TestClass {
public static void main(String[] args){
var k = 2;
do{
System.out.println(k);
}while(--k>0);
}
}

Select 1 option(s):

42.

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

Locale myloc = new Locale.Builder().setLanguage("en").setRegion("UK").build(); //L1
ResourceBundle msgs = ResourceBundle.getBundle("mymsgs", myloc);

Enumeration en = msgs.getKeys();
while(en.hasMoreElements()){
    String key = en.nextElement();
    String val = msgs.getString(key);
    System.out.println(key+" : "+val);
}

Assume that only the following two properties files (contents of the file is shown below the name of the file) are accessible to the code.

1. mymsgs.properties
okLabel=OK
cancelLabel=Cancel   

2. mymsgs_en_UK.properties
okLabel=YES
noLabel=NO

Select 1 option(s):
43.

Question:

Given:

class NewException extends Exception {}

class AnotherException extends Exception {}

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

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

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

}

Which of the following options can be inserted in the above code to make it compile?

Select 3 option(s):
44.

Question: What will the following code print?

public class Test{
   public int luckyNumber(int seed){
      if(seed > 10) return seed%10;
         int x = 0;
            try{
               if(seed%2 == 0) throw new Exception("No Even no.");
               else return x;
            }
            catch(Exception e){
               return 3;
            }
            finally{
               return 7;
            }
         }

        public static void main(String args[]){
           int amount = 100, seed = 6;
           switch( new Test().luckyNumber(6) ){
               case 3: amount = amount * 2;
               case 7: amount = amount * 2;
               case 6: amount = amount + amount;
               default :
           }
          System.out.println(amount);
       }
}

Select 1 option(s):
45.

Question:

Assume that the following directory exists:

c:\a\b\c

A File object is created as follows:

var f = new File("c:\\a\\b\\c\\d\\e");

Given that directories d and e do not exist under c, which of the following statements are correct?

Select 2 option(s):
46.

Question:

Consider the following code:

public class Test extends Thread
{
boolean flag = false;
public Test(boolean f) { flag = f; }
static Object obj1 = new Object();
static Object obj2 = new Object();
public void m1()
{
   synchronized(obj1)
   {
     System.out.print("1 ");
     synchronized(obj2)
     {
       System.out.println("2");
     }
   }
}
public void m2()
{
   synchronized(obj2)
   {
     System.out.print("2 ");
     synchronized(obj1)
     {
       System.out.println("1");
     }
   }
}
public void run()
{
  if(flag){ m1();  m2(); }
  else { m2();  m1(); }
}
public static void main(String[] args)
{
   new Test(true).start();
   new Test(false).start();
}
}

Which of the following statements are correct?

Select 2 option(s):
47.

Question:

Given the following program, which statements are true?

// Filename: TestClass.java
public class TestClass{
   public static void main(String args[]){
      A[] a, a1;
      B[] b;
      a = new A[10]; a1  = a;
      b =  new B[20];
      a = b;  // 1
      b = (B[]) a;  // 2
      b = (B[]) a1; // 3
   }
}
class A { }
class B extends A { }


Select 2 option(s):
48.

Question:

Consider the classes shown below:

class A{
public A() { }
public A(int i) { System.out.println(i ); }
}
class B{
static A s1 = new A(1);
A a = new A(2);
public static void main(String[] args){
var b = new B();
var a = new A(3);
}
static A s2 = new A(4);
}

Which is the correct sequence of the digits that will be printed when B is run?

Select 1 option(s):
49.

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

List<String> list1 = List.of("A", "B");
List<String> list2 = List.copyOf(list1);
list1.add("C"); //1
list2.add("D"); //2
System.out.println(list1+" "+list2);

Select 1 option(s):
50.

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

class Data {
int intVal = 0;
String strVal = "default";
public Data(int k){
this.intVal = k;
}
}
public class TestClass {
public static void main(String[] args) throws Exception {
Data d1 = new Data(10);
d1.strVal = "D1";
Data d2 = d1;
d2.intVal = 20;
System.out.println("d2 val = "+d2.strVal);
}
}

Select 1 option(s):

51.

Question:

Given :

//In Data.java
public class Data{
    int value;
    Data(int value){
        this.value = value;
    }
    public String toString(){ return ""+value; }
}


and the following code fragments:
public  void filterData(ArrayList<Data> dataList, Predicate<Data> p){
   Iterator<Data> i = dataList.iterator();
   while(i.hasNext()){
        if(p.test(i.next())){
             i.remove();
    }
   }
}
....
        ArrayList<Data> al = new ArrayList<Data>();
        Data d = new Data(1); al.add(d);
        d = new Data(2); al.add(d);
        d = new Data(3); al.add(d);

        //INSERT METHOD CALL HERE
       System.out.println(al);


Which of the following options can be inserted above so that it will print [1, 3]?

Select 1 option(s):
52.

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

public class TestClass{
   public static void main(String[] args) {
       int value = 0;
       Supplier<Integer> valueS = ()->value++; //1
       value++;//2
       System.out.println(value+" "+valueS.get()); //3
   }
}


Select 1 option(s):
53.

Question:

Given the following source code, which of the lines that are commented out may be reinserted without introducing errors?

abstract class Bang{
// abstract void f();  // LINE 0
   final    void g(){}
// final    void h(){} // LINE 1
   protected static int i;
   private int j;
}

final class BigBang extends Bang{
// BigBang(int n) { m = n; } // LINE 2
   public static void main(String args[]){
      Bang mc = new BigBang();
   }
// @Override // LINE 3
   void h(){}
// void k(){ i++; } // LINE 4
// void l(){ j++; } // LINE 5
   int m;
}


Consider each line independently.

Select 1 option(s):
54.

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 al = new ArrayList(); //1
al.add(111); //2
System.out.println(al.get(al.size())); //3
}
}

Select 1 option(s):

55.

Question:

Given:

public class Bandwidth{
    public int available = 0;
    public int getAvailable(){
        return available;
    }
    public Bandwidth(int quota){
        this.available = quota;
    }
    public void addMore(int more){
        available += more;
    }
    
}

and a piece of code from another class:
        Bandwidth bw = new Bandwidth(100);
        //INSERT CODE HERE
        System.out.println(bw.getAvailable());

What can be inserted in the code above so that it will print 0?

Select 2 option(s):