Welcome to your Test - 15 Code - 819

Name
Email
Phone
1.

Question: Which of the following implementations of a max() method will correctly return the largest value?

Select 1 option(s):
2.

Question:

MOVE What is the result of compiling and running the following code ?

public class TestClass{
   static int si = 10;
   public static void main (String args[]){
      new TestClass();
   }
   public TestClass(){
      System.out.println(this);
   }
   public String toString(){
      return "TestClass.si = "+this.si;
   }
}

Select 1 option(s):
3.

Question:

Consider the following program...

class Super { }
class Sub extends Super { }
public class TestClass{
public static void main(String[] args){
Super s1 = new Super(); //1
Sub s2 = new Sub(); //2
s1 = (Super) s2; //3
}
}

Which of the following statements are correct?

Select 1 option(s):
4.

Question: What will be the result of compiling and running the following code?

class Base{
public short getValue(){ return 1; } //1
}
class Base2 extends Base{
public byte getValue(){ return 2; } //2
}
public class TestClass{
public static void main(String[] args){
Base b = new Base2();
System.out.println(b.getValue()); //3
}
}

Select 1 option(s):

5.

Question:

Given the following code:

enum Title
{
    MR("Mr. "), MRS("Mrs. "), MS("Ms. ");
    private String title;
    private Title(String s){
         title = s;
    }
    public String format(String first, String last){
         return title+" "+first+" "+last;
    }
}

//INSERT CODE HERE


Identify valid code snippets ..

(Assume that Title is accessible wherever required.)

Select 4 option(s):
6.

Question: Which of the following options are guideline(s) to protect confidential information?

Select 3 option(s):
7.

Question: Which of the following methods are available in java.io.Console?

Select 5 option(s):
8.

Question:

Consider the following program...

class ArrayTest{
   public static void main(String[] args){
      var ia[][] = { {1, 2}, null };
      for (int i = 0; i < 2; i++)
         for (int j = 0; j < 2; j++)
            System.out.println(ia[i][j]);
   }
}

Which of the following statements are true?

Select 1 option(s):
9.

Question:

Given:

String qr = "insert into USERINFO values( ?, ?, ?)";
try(PreparedStatement ps =  c.prepareStatement(qr);)
{
    ps.setObject(0, 1); //1
    ps.setObject(1, "Ally A", JDBCType.VARCHAR); //2
    ps.setObject(2, "101 main str"); //3
    ps.executeUpdate(); //4
    ps.setObject(1, "Bob B"); //5
    ps.executeUpdate(); //6
}


What will be the result?

Select 1 option(s):
10.

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

interface Eatable{
    int types = 10;
}
class Food implements Eatable {
    public static int types = 20;
}
public class Fruit extends Food implements Eatable{  //LINE1
    
    public static void main(String[] args) {
        types = 30; //LINE 2
        System.out.println(types); //LINE 3
    }
}

Select 1 option(s):
11.

Question: What will the following code print?

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

Select 1 option(s):
12.

Question: What can be inserted at line 2 in the code snippet given below?

  String[] names = {"Alex", "Bob", "Charlie" };
  //Insert code here
  System.out.println(list.get(0));


Select 2 option(s):
13.

Question: How can you initialize a StringBuilder to have a capacity of at least 100 characters?

Select 2 option(s):
14.

Question: What will happen on running the following program?

public class DatabaseWrapper
{
  static String url = "jdbc://derby://localhost:1527//mydb";
  static DatabaseWrapper getDatabase()
  {
     System.out.println("Getting DB");
     return null;
  }
  public static void main(String[ ] args)
  {
    System.out.println( getDatabase().url );
  }
}

Select 1 option(s):
15.

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

var numA = new Integer[]{1, 2};
var list1 = List.of(numA);
numA[0] = 2;
var list2 = List.copyOf(list1);
System.out.println(list1+" "+list2);

Select 1 option(s):
16.

Question:

Given the following set of member declarations, which of the following is true?

int a; // (1)
static int a; // (2)
int f( ) { return a; } // (3)
static int f( ) { return a; } // (4)

Select 3 option(s):

17.

Question:

Complete the following code by inserting declaration for stateCitiesMap:

       //Insert line of code here

        List<String> cities = new ArrayList<>();
        cities.add("New York");
        cities.add("Albany");
        stateCitiesMap.put("NY", cities);


Select 2 option(s):
18.

Question:

Given:

Stream<String> names = Stream.of("Sarah Adams", "Suzy Pinnell", "Paul Basgall");
Stream<String> firstNames = //INSERT CODE HERE

Which of the following options will correctly assign a stream of just first names to firstNames?

Select 1 option(s):
19.

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

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

Enumeration<String> 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_hinglish_US.properties
okLabel=OK
cancelLabel=Cancel   

2. mymsgs_hinglish_UK.properties
okLabel=YES
noLabel=NO

Select 1 option(s):
20.

Question: What can be inserted at //2 so that 6 will be printed by the following code?

AtomicInteger ai = new AtomicInteger(5);
//2 INSERT CODE HERE
System.out.println(x);

Select 2 option(s):
21.

Question: Which of the following statements are correct regarding confidential information?


Select 3 option(s):
22.

Question:

Assume the following declarations:

class A{ }
class B extends A{ }
class C extends B{ }

class X{
   B getB(){ return new B(); }
}

class Y extends X{
  //method declaration here
}


Which of the following methods can be inserted in class Y?

Select 2 option(s):
23.

Question:

Given:

import java.io.*;
class TestClass{
   public static void main(String[] args) throws Exception{
        try(var bfr = new BufferedReader(new InputStreamReader(System.in))){
            System.out.println("Enter Number:");
            var s = bfr.readLine();
            System.out.println("Your Number is : "+s);
        }catch(Exception e){
            e.printStackTrace();
        }
   }
}

What will be the output if the above code is executed using the following command:
java TestClass 123

Select 1 option(s):
24.

Question:

Given:

class Base{
  public ArrayList<Number> transform(Set<Integer> list){
      //valid code
  };
}

class Derived extends Base{
  *INSERT CODE HERE*
      //valid code
   }
}

What can be inserted in the above code?

Select 1 option(s):
25.

Question: What happens when you try to compile and run the following program?

public class CastTest{
   public static void main(String args[ ] ){
      byte b = -128 ;
      int i = b ;
      b = (byte) i;
      System.out.println(i+" "+b);
   }
}

Select 1 option(s):
26.

Question:

Given the following command:

javac --module-source-path c:\java\a -d c:\java\b -p  c:\java\c -m x.y


Which of the following statements are correct?

Select 1 option(s):
27.

Question:

Consider :

class A { public void perform_work(){} }
class B extends A { public void perform_work(){} }
class C extends B { public void perform_work(){} }

How can you let perform_work() method of A to be called from an instance method in C?

Select 1 option(s):
28.

Question:

In the following code, after which statement (earliest), the object originally held in s, may be garbage collected ?


1. public class TestClass{
2.   public static void main (String args[]){
3.      Student s = new Student("Vaishali", "930012");
4.      s.grade();
5.      System.out.println(s.getName());
6.      s = null;
7.      s = new Student("Vaishali", "930012");
8.      s.grade();
9.      System.out.println(s.getName());
10      s = null;
     }
   }


public class Student{
   private String name, rollNumber;
   
   public Student(String name, String rollNumber) {
      this.name = name;
      this.rollNumber = rollNumber;
   }

   //valid setter and getter for name and rollNumber follow

   public void grade() {
   }

}


Select 1 option(s):
29.

Question:

Given:

List<Integer> ls = Arrays.asList(1, 2, 3);


Which of the following options will compute the sum of all Integers in the list correctly?

Select 2 option(s):
30.

Question:

Identify examples of autoboxing.

Select 3 option(s):
31.

Question: Which of the following are valid implementations of

java.util.Comparable?

Select 1 option(s):
32.

Question:

Consider the following class definition:

public class TestClass{
public static void main(String[] args){ new TestClass().sayHello(); } //1
public static void sayHello(){ System.out.println("Static Hello World"); } //2
public void sayHello() { System.out.println("Hello World "); } //3
}

What will be the result of compiling and running the class?

Select 1 option(s):
33.

Question: Which of the following are correct definitions of a repeatable annotation?

Select 1 option(s):
34.

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

public class Onion {
    private String data = "skin";
    
    private class Layer extends Onion {
        String data = "thegoodpart";
        public String getData() {
            return data;
        }
    }

    public String getData() {
        return new Layer().getData();
    }

    public static void main(String[] args) {
        var o = new Onion();
        System.out.println(o.getData());
    }
}

Select 1 option(s):
35.

Question:

Given:

class Item{
    private int id;
    private String name;
    public Item(int id, String name){
        this.id = id;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String toString(){
      return name;
    }
}


public class Test {
    public static void main(String[] args) {
        List<Item> l = Arrays.asList(
                new Item(1, "Screw"),
                new Item(2, "Nail"),
                new Item(3, "Bolt")
        );

       l.stream()
//INSERT CODE HERE
      .forEach(System.out::print);        
    }
}

Which of the following options can be inserted in the above code independent of each other, so that the code will print BoltNailScrew?

Select 2 option(s):
36.

Question:

Identify examples of auto-unboxing.

Select 2 option(s):
37.

Question:

Given:

enum Card
{
    HEART, CLUB, SPADE, DIAMOND;
}


Which of the following code fragments will print
HEART CLUB SPADE DIAMOND
in that order?

Select 1 option(s):
38.

Question: What will be the output of the following code snippet?

int a = 1;
int[] ia = new int[10];
int b = ia[a];
int c = b + a;
System.out.println(b = c);

Select 1 option(s):
39.

Question: What will the following statement return?

"    hello java guru   ".strip();

Select 1 option(s):
40.

Question:

Consider the following method -

public float parseFloat( String s ){
   float f = 0.0f;
   try{
      f = Float.valueOf( s ).floatValue();
      return f ;
   }
   catch(NumberFormatException nfe){
      f = Float.NaN ;
      return f;
   }
   finally{
      f = 10.0f;
      return f;
   }
}

What will it return if the method is called with the input "0.0" ?

Select 1 option(s):
41.

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

public class TestClass{
long l1;
public void TestClass(long pLong) { l1 = pLong ; } //(1)
public static void main(String args[]){
TestClass a, b ;
a = new TestClass(); //(2)
b = new TestClass(5); //(3)
}
}

Select 1 option(s):

42.

Question:

Consider the following code:

public class FileCopier {
    public static void copy(String records1, String records2) throws IOException {
        try (
                InputStream is = new FileInputStream(records1);
                OutputStream os = new FileOutputStream(records2);) {
            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);
            }
        } catch (IOException | IndexOutOfBoundsException e) {
            e = new FileNotFoundException();
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        copy("c:\\temp\\test1.txt", "c:\\temp\\test2.txt");
    }
}


Assuming appropriate import statements and the existence of both the files, what will happen when the program is compiled and run?

Select 1 option(s):
43.

Question:

Given:

var sList = new CopyOnWriteArrayList<Student>();


Which of the following statements are correct?

Select 1 option(s):
44.

Question:

Given:

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

What is the status of Device objects at the end of the try/catch blocks?

Select 1 option(s):
45.

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

public class TestClass{
   public static void main(String args[]){
      Exception e = null;
      throw e;
   }
}

Select 1 option(s):
46.

Question:

Given:

String[] p = {"1", "2", "3" };


Which of the following lines of code is/are valid?

Select 1 option(s):
47.

Question:

Identify correct statements about Java exceptions.

Select 1 option(s):
48.

Question:

Given:

class Base{
  public List<CharSequence> transform(Set<CharSequence> list){
      //valid code
  };
}

class Derived extends Base{
  *INSERT CODE HERE*
      //valid code
   }
}

What can be inserted in the above code?

Select 2 option(s):
49.

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

BiPredicate<String, Integer> bip = (s, i)-> s.length()>i;  //1
BiFunction<String, Integer, String> bif = (s, i)-> {  //2
   if(bip.test(s, i)){ //3
      return s.substring(0, i);
   }
   else return s;
};
String str = bif.apply("hello world", 5); //4
System.out.println(str);

Select 1 option(s):
50.

Question: Which statements can be inserted at line 1 in the following code to make the program write x on the standard output when run?

public class AccessTest{
String a = "x";
static char b = 'x';
String c = "x";
class Inner{
String a = "y";
String get(){
String c = "temp";
// Line 1
return c;
}
}
AccessTest() {
System.out.println( new Inner().get() );
}
public static void main(String args[]) { new AccessTest(); }
}

Select 3 option(s):

51.

Question:

Your application was developed and packaged on the Java 8 platform as a jar file. It consists of the following packages:

com.abc.stocks
com.abc.stocks.valuation
com.abc.stocks.analytics
com.abc.bonds
com.abc.bonds.valuation
com.abc.bonds.analytics
com.abc.derivatives
com.abc.derivatives.valuation
com.abc.derivatives.analytics


You are now trying to modularize it into three different modules. Which of the following module descriptions could potentially be valid?

Select 1 option(s):
52.

Question:

Given the following exception classes:

class MyException extends Exception{}
class MyException1 extends MyException{}
class MyException2 extends MyException{}
class MyException3 extends MyException2{}

and the following code:

      try{
         //code that could potentially throw any of the above mentioned exceptions
      }
      INSERT CODE HERE


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

Select 1 option(s):
53.

Question:

Given:

StringBuilder sb = new StringBuilder("asdf");


Which of the following code fragments will print true?

Select 1 option(s):
54.

Question: What will the following code print ?

class Test{
   public static void main(String[] args){
      int k = 1;
      int[] a = { 1 };
      k += (k = 4) * (k + 2);
      a[0] += (a[0] = 4) * (a[0] + 2);
      System.out.println( k + " , " + a[0]);
   }
}

Select 1 option(s):
55.

Question:

Given:

class Booby{
}
class Dooby extends Booby{
}
class Tooby extends Dooby{
}


and the following declarations:

List<? super Booby> bV = null;
List<? extends Tooby> tV = null;


Which of the following statements will compile without any error?

Select 1 option(s):