Welcome to your Test - 10 Code - 819

Name
Email
Phone
1.

Question:

Consider the following class written by a novice programmer.

class Elliptical{
public int radiusA, radiusB;
public int sum = 100;
public void setRadius(int r){
if(r>99) throw new IllegalArgumentException();
radiusA = r;
radiusB = sum - radiusA;
}
}

After some time, the requirements changed and the programmer now wants to make sure that radiusB is always (200 - radiusA) instead of (100 - radiusA) without breaking existing code that other people have written. Which of the following will accomplish his goal?

Select 1 option(s):
2.

Question:

Consider the following classes:

class A implements Runnable{ ...}
class B extends A implements Observer { ...}
(Assume that Observer has no relation to Runnable.)

and the declarations :

  A a = new A() ;
  B b = new B();

Which of the following Java code fragments will compile and execute without throwing exceptions?

Select 2 option(s):
3.

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

import java.io.Serializable;
class Booby{
    int i; public Booby(){ i = 10; System.out.print("Booby"); }
}
class Dooby extends Booby implements Serializable {
    int j; public Dooby(){ j = 20; System.out.print("Dooby"); }
}
class Tooby extends Dooby{
    int k; public Tooby(){ k = 30; System.out.print("Tooby"); }
}
public class TestClass {
  public static void main(String[] args) throws Exception{
    
    var t = new Tooby();
    t.i = 100;
    var oos  = new ObjectOutputStream(new FileOutputStream("c:\\temp\\test.ser"));
    oos.writeObject(t); oos.close();
    
    var ois = new ObjectInputStream(new FileInputStream("c:\\temp\\test.ser"));
    t = (Tooby) ois.readObject();ois.close();
    
    System.out.println(t.i+" "+t.j+" "+t.k);
  }
}

Select 1 option(s):
4.

Question: What will be the output of the following class.

class Test{
   public static void main(String[] args){
      int j = 1;
      try{
         int i = doIt() / (j = 2);
      } catch (Exception e){
         System.out.println(" j = " + j);
      }
   }
   public static int doIt() throws Exception {  throw new Exception("FORGET IT");  }
}

Select 1 option(s):
5.

Question:

Consider the following code in which searchBook() method has an inner class.

public class BookStore
{
   private static final int taxId = 300000;
   private String name;
   public String searchBook( final String criteria )
   {
      int count = 0;
      int sum = 0;
      sum++;
      class Enumerator
      {
         String iterate( int k)
         {
            //line 1
            return "";
         }
         // lots of code.....
      }
      // lots of code.....
      return "";
   }
}

Which variables are accessible at line 1?

Select 5 option(s):
6.

Question: Which line in the following code will cause the compilation to fail?

public class TestClass {
    
    public static void main(String[] args) throws Exception {
       work();                  //LINE 10
       int j = j1;               //LINE 11
       int j1 = (double) x; //LINE 12
    }
   
    public static void work() throws Exception{
        System.out.println(x); //LINE 15
    }

    static double x;    //19
}

Select 2 option(s):
7.

Question: Which of these assignments are valid?

Select 3 option(s):
8.

Question:

Consider the following lines of code:

boolean greenLight = true;
boolean pedestrian = false;
boolean rightTurn = true;
boolean otherLane = false;

You can go ahead only if  the following expression evaluates to 'true' :

(( (rightTurn && !pedestrian || otherLane) || ( ? && !pedestrian && greenLight ) )  == true )

What variables can you put in place of '?' so that you can go ahead?

Select 1 option(s):
9.

Question:

Consider the following class...

public class ParamTest {
  
  public static void printSum(double a, double b){
      System.out.println("In double "+(a+b));
  }
  public static void printSum(float a, float b){
      System.out.println("In float "+(a+b));
  }
  
  public static void main(String[] args) {
      printSum(1.0, 2.0);
  }
}

What will be printed?

Select 1 option(s):
10.

Question:

Consider the following code :

        Connection connection = dataSource.getConnection();
        PreparedStatement stmt = connection.prepareStatement("select * from CUSTOMER where EMAILID=?");
        stmt.setObject(1, "bob@gmail.com"); //LINE 10
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            System.out.println(rs.getString("EMAILID")); //LINE 12
        }
        connection.close();


Assuming that the query returns exactly 1 row, what will be printed when this code is run?

(Assume that items not specified such as import statements, DB url, and try/catch block are all valid.)

Select 1 option(s):
11.

Question:

Consider:

public class Outer
{
int i = 10;
class Inner
{
public void methodA()
{
//line 1.
}
}
}

Which of the following statements are valid at line 1 (Select the best answer).

Select 2 option(s):
12.

Question: Which of the following four constructs are valid?

1.
   switch(5)
  {
      default :
   }

2.
   switch(5)
   {
      default : break;
   }

3.
  switch(8);

4.
var x = 0;
switch(x){
}

Select 1 option(s):
13.

Question:

Given:

public class Square {
private double side = 0; // LINE 2
public static void main(String[] args) { // LINE 4
Square sq = new Square(); // LINE 5
side = 10; // LINE 6
}
}

What can be done to make this code compile and run?

Select 1 option(s):
14.

Question: What will the following program snippet print?

   int i=0, j=11;
   do{
      if(i > j) { break; }
      j--;
   }while( ++i < 5);
   System.out.println(i+"  "+j);

Select 1 option(s):
15.

Question: Which of the following statements would you need to have in the module-info of a Java Swing based desktop application?

Select 1 option(s):
16.

Question: What will the following code fragment print?

        Path p1 = Paths.get("\\personal\\readme.txt");
        Path p2 = Paths.get("\\index.html");
        Path p3 = p1.relativize(p2);
        System.out.println(p3);

Select 1 option(s):
17.

Question:

Given:

class Triangle{
public int base;
public int height;
private static double ANGLE;
public static double getAngle();
public static void Main(String[] args) {
System.out.println(getAngle());
}
}

Identify the correct statements:

Select 1 option(s):
18.

Question: What will the following code print?

        int[] a = { 'h', 'e', 'l'};
        int[] b = { 'h', 'e', 'l', 'l', 'o'};
        
        int x = Arrays.compare(a, b);
        int y = Arrays.mismatch(a, b);
        System.out.println(x+" "+y);

Select 1 option(s):
19.

Question: Which of the following statements are acceptable?

Select 4 option(s):
20.

Question:

The following are complete contents of TestClass.java:

class Book{
  protected final int pages = 100;
  final void mA(){
     System.out.println("In B.mA "+pages);
  }
}

class Encyclopedia extends Book{
  private int pages = 200; //1
  void mB(){
     System.out.println("In E.mB "+pages);       
  }
  void mA(){ //2
     System.out.println("In E.mA "+pages);
  }
}
public class TestClass {
  public static void main(String[] args) {
    Book o1 = new Encyclopedia (); //3
    Book o2 = new Book();
    o1.mA(); //4
    o1.mB(); //5
    o2.mA();
  }
}

Which lines will cause compilation to fail?

Select 1 option(s):
21.

Question:

Given:

BiFunction<String, Integer, String> bf = (String s, Integer i) -> { return s.substring(0, i); };

What of the following lambda expressions is equivalent to the one used above?

Select 1 option(s):
22.

Question: Which of these statements are true?

Select 2 option(s):
23.

Question:

Consider these two interfaces:

interface I1
{
   void m1() throws java.io.IOException;
}
interface I2
{
   void m1() throws java.io.FileNotFoundException;
}


Which of the following are valid method declarations for a class that says it implements I1 and I2 ?

Select 1 option(s):
24.

Question:

Consider the following code:

//Assume appropriate imports
class Person{
    String name;
    String dob;
    public Person(String name, String dob){
        this.name = name; this.dob = dob;
    }
}
public class SortTest {
    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

        for(Person a : al) System.out.println(a.name+" "+ a.dob);
    }
}


What can be inserted in the code so that it will sort the collection of Persons by Person's dob attribute?

Select 1 option(s):
25.

Question:

Consider the following class...

class TestClass{
void probe(Integer x) { System.out.println("In Integer"); } //2
void probe(Object x) { System.out.println("In Object"); } //3
void probe(Long x) { System.out.println("In Long"); } //4
public static void main(String[] args){
String a = "hello";
new TestClass().probe(a);
}
}

What will be printed?

Select 1 option(s):
26.

Question:

Given the following pairs of method declarations, which of the statements are true?

1.
void perform_work(int time){ }
int  perform_work(int time, int speed){ return time*speed ;}

2.
void perform_work(int time){ }
int  perform_work(int speed){return speed ;}

3.
void perform_work(int time){ }
void Perform_work(int time){ }


Select 2 option(s):
27.

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 {
ArrayList<String> al = new ArrayList<String>();
al.add("111");
al.add("222");
System.out.println(al.get(al.size()));
}
}

Select 1 option(s):

28.

Question:

Given the following class:

class Person{
  private String name;
  private java.time.LocalDate dob;
  
  public Person(String name, java.time.LocalDate dob){
     this.name = name;
     this.dob = dob;
  }
  //public getters and setters for name and dob

}


and the following method appearing in some other class:

public List<Person> getEligiblePersons(List<Person> people){
    var pl = new ArrayList<Person>();
    for(var p : people){
        if(p.getDob().isBefore(cutoff)){
            pl.add(p);
        }
    }
    return pl;
}


Select 2 option(s):
29.

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):
30.

Question: Given a class named Test, which of these would be valid definitions for a constructor for the class?

Select 1 option(s):
31.

Question:

Given the following method:

public static void copy(String fileName1, String fileName2) throws Exception{
    try (
            InputStream is = new FileInputStream(fileName1);
            OutputStream os = new FileOutputStream(fileName2);  ) {
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
            System.out.println("Read and written bytes " + bytesRead);
        }
    }
}    


What will happen fileName1 contains only 100 bytes and fileName2 contains 200 bytes?

Select 1 option(s):
32.

Question:

Given:

List<String> strList = Arrays.asList("a", "aa", "aaa");
Function<String, Integer> f = x->x.length();
Consumer<Integer> c = x->System.out.print("Len:"+x+" ");
strList.stream().map(f).forEach(c);

What will it print when compiled and run?

Select 1 option(s):
33.

Question:

Given:

class Automobile{
   final String getVin(){ return "1234"; }
}


Identify correct statements.

Select 1 option(s):
34.

Question:

Given:

ConcurrentMap<String, Object> cache = new ConcurrentHashMap<>();


Which of the given statements are correct about the following code fragment:

if(!cache.containsKey(key)) cache.put(key, value);

(Assume that key and value refer to appropriate objects.)

Select 1 option(s):
35.

Question: What will the following class print when executed?

class Test{
    static boolean a;
    static boolean b;
    static boolean c;
    public static void main (String[] args){
        boolean bool = (a = true) || (b = true) && (c = true);
        System.out.print(a + ", " + b + ", " + c);
    }
}

Select 1 option(s):
36.

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

public class DaysTest{

    static String[] days = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
    
    public static void main(String[] args) {
        
        var index = 0;
        for(var day : days){
            
            if(index == 3){
                break;
            }else {
                 continue;
            }
            index++;
            if(days[index].length()>3){
                days[index] = day.substring(0,3);
            }
        }
        System.out.println(days[index]);
    }
}


Select 1 option(s):
37.

Question:

Consider the following code to count objects and save the most recent object:

int i = 0 ;
Object prevObject ;
public void saveObject(List e ){
    prevObject = e ;
    i++ ;
}


Which of the following calls will work without throwing an exception?

Select 3 option(s):
38.

Question:

Given:

class Base{
   public  <T extends Number, Z extends Number> Map<T, Z> getMap(T t, Z z)
   {
      return new HashMap<T, Z>();
   }
}

class Derived extends Base{
   //public  <T, Z> TreeMap<T, Z> getMap(T t, Z z) { return new TreeMap<T, Z>(); }; //1

   //public  Map<Number, Number> getMap(Number t, Number z) { return new TreeMap<Number, Number>(); }; //2

   //public  Map<Integer, Integer> getMap(Number t, Number z) { return new HashMap<Integer, Integer>(); };   //3
}


Identify correct statements about the methods defined in Derived assuming they are uncommented one at a time individually.

Select 1 option(s):
39.

Question:

Given that Book is a valid class with appropriate constructor and getTitle and getPrice methods that return a String and a Double respectively, what can be inserted at //1 and //2 so that it will print the price of all the books having a title that starts with "A"?

List<Book> books = Arrays.asList(
        new Book("Atlas Shrugged", 10.0),
        new Book("Freedom at Midnight", 5.0),
        new Book("Gone with the wind", 5.0)
);

Map<String, Double> bookMap = //1 INSERT CODE HERE
//2 INSERT CODE HERE
bookMap.forEach(func);


Select 1 option(s):
40.

Question:

Given:

class Person{
  private String name;
  private java.util.Date dob;
  
  public Person(String name, java.util.Date dob){
     this.name = name;
     this.dob = (java.util.Date) dob.clone();
     if(!validateDob(this.dob)) throw new IllegalArgumentException("dob cannot be in future");
  }
  
  protected boolean validateDob(java.util.Date dob){
      return !dob.after(new java.util.Date());
  }

  //other methods not shown

}

What changes should be made to make this class immutable?

Select 2 option(s):
41.

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

    public static void main(String[] args) {
        Float f1 = 10.0f;
        Float f2 = 0.0f;

        Float f3 = null;
        
        double f = 0.0;
        try{
            f = f1/f2;
            System.out.println(f);
            f3 = f1/f2;
        }catch(Exception e){
            System.out.println("Exception");
        }
        System.out.println(f3.isInfinite());
    }

Select 1 option(s):
42.

Question: Which of the following statements can be inserted at // 1 to make the code compile without errors?

public class InitTest{
static int si = 10;
int i;
final boolean bool;
// 1
}

Select 1 option(s):

43.

Question:

Consider the following class...

class TestClass{
   int x;
   public static void main(String[] args){
      // lot of code.
   }
}


Select 1 option(s):
44.

Question:

Given:

  Connection con = DriverManager.getConnection(dbURL);
  con.setAutoCommit(false);
  String updateString =
        "update SALES " +
        "set T_AMOUNT = 100 where T_NAME = 'BOB'";
  Statement stmt = con.createStatement();
  stmt.executeUpdate(updateString);
  //INSERT CODE HERE


What statement should be added to the above code so that the update is committed to the database?

Select 1 option(s):
45.

Question:

Given:

double daaa[][][] = new double[3][][];
var d = 100.0;
double[][] daa = new double[1][1];


Which of the following statements can be added to the above code, independent of each other, without causing compilation or runtime errors?

Select 3 option(s):
46.

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

public class Discounter {
    static double percent; //1
    int offset = 10, base= 50; //2
    public static double calc(double value) {
        var coupon, offset, base; //3
        if(percent <10){ //4
            coupon = 15;
            offset = 20;
            base = 10;
        }
        return coupon*offset*base*value/100; //5
    }
    public static void main(String[] args) {
        System.out.println(calc(100));
    }
}

Select 1 option(s):
47.

Question:

Given:

Path p1 = Paths.get("c:\\a\\b\\c.java");


What will p1.getName(2).toString() return?

Select 1 option(s):
48.

Question:

Given the following class code:

public class Transfer implements Runnable{
    Account from, to;
    double amount;
    public Transfer(Account from, Account to, double amount){
        this.from = from; this.to = to; this.amount = amount;
    }
    public void run(){
        synchronized(from){
            from.setBalance(from.getBalance()-amount);
            synchronized(to){
                to.setBalance(to.getBalance()+amount);
            }
        }
    }
}

Relevant code for the Account class referred above is as follows:
public class Account{
    private String id; private double balance;
    //constructor and accessor methods not shown,
}


What will happen when the following code is executed?


var es = Executors.newCachedThreadPool();
var a1 = new Account("A1", 1000);
var a2 = new Account("A1", 1000);
es.submit(new Transfer(a1, a2, 200));
es.submit(new Transfer(a2, a1, 300));


Select 1 option(s):
49.

Question:

Consider the following code:

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class MyCache {
    private CopyOnWriteArrayList<String> cal = new CopyOnWriteArrayList<>();

    public void addData(List<String> list){
        cal.addAll(list);
    }
    
    public Iterator getIterator(){
        return cal.iterator();
    }
}


Given that one thread calls the addData method on an instance of the above class and another thread calls the getIterator method on the same instance at the same time and starts iterating through its values, which of the following options are correct?


(Assume that no other calls have been made on the MyCache instance.)

Select 1 option(s):
50.

Question:

Consider the following code:

package jqplus;
import java.util.*;

public class Testclass {

    public static void main(String[] args) {

        NavigableMap<String, String> mymap = new TreeMap<String, String>();
        mymap.put("a", "apple"); mymap.put("b", "boy"); mymap.put("c", "cat");
        mymap.put("aa", "apple1"); mymap.put("bb", "boy1"); mymap.put("cc", "cat1");

        mymap.pollLastEntry(); //LINE 1
        mymap.pollFirstEntry(); //LINE 2
        
        NavigableMap<String, String> tailmap = mymap.tailMap("bb", false); //LINE 3

        System.out.println(tailmap.pollFirstEntry()); //LINE 4
        System.out.println(mymap.size()); //LINE 5
        
    }
}


What will be returned by the call to tailmap.pollFirstEntry() at //LINE 4 and mymap.size() at //LINE 5?

Select 1 option(s):
51.

Question:

Identify the correct statements about the following code fragment:

        var fw = new FileWriter("c:\\temp\\test.txt");
        var bfw = new BufferedWriter(fw);
        bfw.writeUTF("hello");  //1
        bfw.newLine();  //2
        bfw.write("world");  //3


Select 1 option(s):
52.

Question:

Given:

public class TableTest {
    static String[][] table;
    public static void main(String[] args) {
        String[] x = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
        String[] y1 = { "1", "2", "3", "4", "5" };
        String[] y2 = { "a", "b", "c" };
        
        table = new String[3][];
        table[0] = x;
        table[1] = y1;
        table[2] = y2;

        //INSERT CODE HERE

    }
}


What can be inserted in the above code to make it print Sun5c?

Select 1 option(s):
53.

Question:

Given:

public class Person{
  private String name;
  private java.util.Date dob;
  public Person(String name, java.util.Date dob){
     this.name = name;
     this.dob = dob;
  }

   public  String getName(){ return name; }
   public  java.util.Date getDob(){ return dob; }
}


What, if anything, is wrong with the above code from secure coding guidlines perspective?

Select 1 option(s):
54.

Question:

Consider the following code:

import java.util.*;
public class TestClass
{    
    static String[] sa = { "charlie", "bob", "andy", "dave" };
    public static void main(String[] args)
    {
        // 1 insert code here.
    }
}


Which of the given options (independent of each other or together) must be inserted at //1 so that it will print 2?

Select 2 option(s):
55.

Question: What will be printed when the following code is compiled and run?

package trywithresources;

import java.io.IOException;

public class Device implements AutoCloseable{
    String header = null;
    public void open(){
        header = "OPENED";
        System.out.println("Device Opened");
    }
    public String read() throws IOException{
        throw new IOException("Unknown");
    }
    
    public void writeHeader(String str) throws IOException{
        System.out.println("Writing : "+str);
        header = str;
    }
    public void close(){
        header = null;
        System.out.println("Device closed");
    }
    
    public static void testDevice(){
        Device d = new Device();
        try(d){
            d.open();
            d.read();
            d.writeHeader("TEST");
            d.close();
        }catch(IOException e){
            System.out.println("Got Exception");
        }
    }
    public static void main(String[] args) {
        Device.testDevice();
    }
    
}

Select 1 option(s):