Welcome to your Test - 3 Code - 819

Name
Email
Phone
1.

Question: What will the following code print?

List<String> vowels = new ArrayList<String>();
vowels.add("a");
vowels.add("e");
vowels.add("i");
vowels.add("o");
vowels.add("u");
Function<List<String>, List<String>> f = list->list.subList(2, 4);
f.apply(vowels);
vowels.forEach(System.out::print);

Select 1 option(s):
2.

Question:

Consider the following code:

static void doElements(List l) {
    l.add("string"); //1
   System.out.println(l.get(0)); //2
}


Select 1 option(s):
3.

Question:

Consider the following code appearing in a module-info.java

module com.amazing.movies{
   requires transitive com.amazing.customer;
}

Identify correct statements.

Select 1 option(s):
4.

Question:

Identify correct statements about the following code:

class Writer{
    private static final int LOOPSIZE = 5;
    public synchronized void write(Data... da){
        for(int i=0; i<LOOPSIZE; i++){
            while(!da[0].own(this));
            while(!da[1].own(this));
            da[0].write(i); da[1].write(i);
            da[1].release();
            da[0].release();
        }
    }
}
class Data{
    Writer writer;  int id;
    public Data(int id){ this.id = id; }
    public synchronized boolean own(Writer w){
        if(writer == null){
            writer  = w; return true;
        }
        else return false;
    }
    public synchronized void release(){ writer = null; }
    public void write(int i){
        System.out.println("data written W"+i+" D"+id);
    }
}
public class TestClass {
    public static void main(String[] args) {
        var w1 = new Writer();
        var w2 = new Writer();
        var d1 = new Data(1);
        var d2 = new Data(2);
        new Thread(()->{w1.write(d1, d2);}).start();
        new Thread(()->{w2.write(d1, d2);}).start();
    }
}


Select 1 option(s):
5.

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

Select 1 option(s):
6.

Question:

Given a Course class with appropriate constructor and methods and the following code:

List<Course> s1 = //create a list with a few Course objects
String testCategory = "Java";
Integer comparisonCode = 1;
long l = s1.stream()
        .peek(new Consumer<Course>(){
                    public void accept(Course c){
                         c.printPassPercent();
                    }})
        .map(c -> testCategory.compareToIgnoreCase(c.getCategory()))
        .filter(a->a == 0)
        .count();
System.out.println(l);


Which of the method arguments can be changed to use method references?

Select 2 option(s):
7.

Question:

Given:

List<String> letters = Arrays.asList("j", "a", "v","a");
String word = INSERT CODE HERE
System.out.println(word);

Which of the following code fragments when inserted in the above code independent of each other will cause it to print java?

Select 1 option(s):
8.

Question: Which of the following interface definitions can be implemented using Lambda expressions?

@FunctionalInterface
interface A{
   static void m1(){};
}

interface AA extends A{
   void m2();
   private void mp(){ }
}

interface AAA extends AA{
   void m3();
}

interface B {
   default void m1(){  }
}
interface BB extends B {
  static void m2(){ };
}
interface C extends BB{
    void m3();
}

Select 2 option(s):
9.

Question:

Given:

public @interface Authors{
   Author[] value();
}
@Repeatable(Authors.class)
public @interface Author {
    int id() default 0;
    String value();
}


Identify correct usages of the above annotations.

Select 2 option(s):
10.

Question: What can be the return type of method getSwitch so that this program compiles and runs without any problems?

public class TestClass{
public static XXX getSwitch(int x){
return x - 20/x + x*x;
}
public static void main(String args[]){
switch( getSwitch(10) ){
case 1 :
case 2 :
case 3 :
default : break;
}
}
}

Select 1 option(s):

11.

Question:

Given:

public void processUploads(String listOfFilesFile) throws IOException{
    //1
    File f = new File(listOfFilesFile);
    List al = new ArrayList();
    try(var bfr = new BufferedReader(new FileReader(f))){
        //2
        String uploadedFileName = null;
        while( (uploadedFileName=bfr.readLine()) != null ){
            var fbfr = new BufferedReader(new FileReader(uploadedFileName));
            al.add(fbfr);
            //3
            loadXML(fbfr);
            //4
        }
    }finally{
        for(var openReader : al){
            try{ openReader.close(); }catch(Exception e)
            { e.printStackTrace();}
        }

    }
}

Which of the following actions will limit a potential denial of service attack on the above code?

Select 2 option(s):
12.

Question:

Given:

public class Counter{
    static AtomicInteger ai = new AtomicInteger(0);
    public static void increment(){
       //1
    }
    //other valid code
}


This class is supposed to keep an accurate count for the number of times the increment method is called. Several classes share this class and call its increment method. What should be inserted at //1 ?

Select 2 option(s):
13.

Question: What will the following code print?

  int i = 0;
  int j = 1;
  if( (i++ == 0) & (j++ == 2) ){
     i = 12;
  }
  System.out.println(i+" "+j);

Select 1 option(s):
14.

Question: What is the result of compiling and running the following program?

public class Learner {
    public static void main(String[] args) {
        var dataArr = new String[4];
        dataArr[1] = "Bill";
        dataArr[2] = "Steve";
        dataArr[3] = "Larry";
        try{
            for(String data : dataArr){
                System.out.print(data+" ");
            }
        }catch(Exception e){
            System.out.println(e.getClass());
        }
    }
}

Select 1 option(s):
15.

Question: Which of the following statements are correct about --jdk-internals option of jdeps tool when run on a jar file or a class file?

Select 2 option(s):
16.

Question: Which of the following code snippets will print exactly 10?

1.  Object t = new Integer(106);
    int k = ((Integer) t).intValue()/10;
    System.out.println(k);

2.  System.out.println(100/9.9);

3.  System.out.println(100/10.0);

4.  System.out.println(100/10);

5.  System.out.println(3 + 100/10*2-13);

Select 3 option(s):
17.

Question: What will the following code print when run?

       Deque<Integer> d = new ArrayDeque<>();
        d.add(1);
        d.add(2);
        d.addFirst(3);
        System.out.println(d.pollLast());
        System.out.println(d.pollLast());
        System.out.println(d.pollLast());

Select 1 option(s):
18.

Question: You want to change the default locale for formatting numbers, currency, and percentages. Which of the following statements will achieve that?

Select 2 option(s):
19.

Question: Given the following code snippet:

int rate = 10;
int t = 5;
XXX amount = 1000.0;
for(int i=0; i<t; i++){
amount = amount*(1 - rate/100);
}

What can XXX be?

Select 1 option(s):
20.

Question:

Consider the following code:

public class ArrayTest {

    static int[][] table = new int[2][3];

    public static void init() {
        for (var x = 0; x < table.length; x++) {
            for (var y = 0; y < table[x].length; y++) {

                //insert code to initialize

            }
        }
    }

    public static void multiply() {
        for (var x = 0; x < table.length; x++) {
            for (var y = 0; y < table[x].length; y++) {

                //insert code to multiply

            }
        }
    }
}


Which of the following options can be used in the code above so that the init method initializes each table element to the sum of its row and column number and the multiply method just multiplies the element value by 2?

Select 1 option(s):
21.

Question:

Given:

Path p  = Paths.get("c:\\temp\\out");
try{
    var b = Files.deleteIfExists(p);
    System.out.println(b);
}catch(Exception e){
    e.printStackTrace();
}

Identify correct statements.

Select 1 option(s):
22.

Question:

The following class will print 'index = 2' when compiled and run.

class Test{
   public static int[ ] getArray() {  return null;  }
   public static void main(String[] args){
      var index = 1;
      try{
         getArray()[index=2]++;
      }
      catch (Exception e){  }  //empty catch
      System.out.println("index = " + index);
   }
}


Select 1 option(s):
23.

Question:

Given:

  public List<? extends Number> getList(){
      *INSERT CODE HERE*
  }


What can be inserted in the above code?

Select 3 option(s):
24.

Question: Which statements about the output of the following programs are true?

public class TestClass{
public static void main(String args[ ] ){
int i = 0 ;
boolean bool1 = true;
boolean bool2 = false;
boolean bool = false;
bool = (bool2 & method1("1")); //1
bool = (bool2 && method1("2")); //2
bool = (bool1 | method1("3")); //3
bool = (bool1 || method1("4")); //4
}
public static boolean method1(String str){
System.out.println(str);
return true;
}
}

Select 2 option(s):

25.

Question: Which of the following statements are correct regarding synchronization and locks?

Select 1 option(s):
26.

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

public class TestClass{
   public static void main(String args[]){
      try{
         m1();
      }catch(IndexOutOfBoundsException e){
         System.out.println("1");
         throw new NullPointerException();
      }catch(NullPointerException e){
         System.out.println("2");
         return;
      }catch (Exception e) {
         System.out.println("3");
      }finally{
         System.out.println("4");
      }
      System.out.println("END");
   }

   static void m1(){
      System.out.println("m1 Starts");
      throw new IndexOutOfBoundsException( "Big Bang " );
   }
}

Select 3 option(s):
27.

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

package exceptions;
public class TestClass {
    public static void main(String[] args) {
        try{
            doTest();
        }
        catch(MyException me){
            System.out.println(me);
        }
    }
    
    static void doTest() throws MyException{
        int[] array = new int[10];
        array[10] = 1000;
        doAnotherTest();
    }
    
    static void doAnotherTest() throws MyException{
        throw new MyException("Exception from doAnotherTest");
    }
}
class MyException extends Exception {
    public MyException(String msg){
     super(msg);
    }
}

(Assume that there is no error in the line numbers given in the options.)

Select 1 option(s):
28.

Question:

Given that test1.txt exists but test2.txt doesn't exist, consider the following code?

public class FileCopier {
    
    public static void copy1(Path p1, Path p2) throws Exception {
        Files.copy(p1, p2, StandardCopyOption.COPY_ATTRIBUTES);
    }
    
    
    public static void main(String[] args) throws Exception {
        Path p1 = Paths.get("c:\\temp\\test1.txt");
        Path p2  = Paths.get("c:\\temp\\test2.txt");
        copy1(p1, p2);
    }
}


Which file attributes will be copied over to test2.txt?

Select 1 option(s):
29.

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

class TestClass{
   public static void main(String args[] ){
      String str1 = "str1";
      String str2 = "str2";
      System.out.println( str1.concat(str2) );
      System.out.println(str1);
   }
}

Select 1 option(s):
30.

Question:

Consider the following code snippet :

   public void readData(String fileName) throws Exception {
        try(FileReader fr1 = new FileReader(fileName)){
            Connection c = getConnection();
            //...other valid code
        }
    }
    


Given that getConnection method throws ClassNotFoundException and that an IOException is thrown while closing fr1, which exception will be received by the caller of readData() method?

Select 1 option(s):
31.

Question: What can be inserted into the following code at //1 so that it will print "Oldboy"?

//imports not shown
class Movie{
    static enum Genre  {DRAMA, THRILLER, HORROR, ACTION };
    private Genre genre;
    private String name;
    Movie(String name, Genre genre){
        this.name = name; this.genre = genre;
    }
    //accessors not shown
}
public class FilteringStuff {
    public static void main(String[] args) {
        List<Movie> movies = Arrays.asList(
                new Movie("On the Waterfront", Movie.Genre.DRAMA),
                new Movie("Psycho", Movie.Genre.THRILLER),
                new Movie("Oldboy", Movie.Genre.THRILLER),
                new Movie("Shining", Movie.Genre.HORROR)
                );
        Predicate<Movie> horror = mov->mov.getGenre() == Movie.Genre.THRILLER;
        Predicate<Movie> name = mov->mov.getName().startsWith("O");
        //1 INSERT CODE HERE
        .forEach(mov->System.out.println(mov.getName()));
    }
    
}

Select 1 option(s):
32.

Question:

Given the following code:

        var raf = new RandomAccessFile("c:\\temp\\test.txt", "rwd");
        raf.writeChars("hello");
        raf.close();


Which of the following statements are correct?
(Assume that the code has appropriate security permissions.)

Select 1 option(s):
33.

Question: What can be inserted at //1 and //2 in the code below so that it can compile without errors:

class Doll{
String name;
Doll(String nm){
this.name = nm;
}
}
class Barbie extends Doll{
Barbie(){
//1
}
Barbie(String nm){
//2
}
}
public class TestClass {
public static void main(String[] args) {
Barbie b = new Barbie("mydoll");
}
}

Select 2 option(s):

34.

Question:

You want to use a module built by another team in your company. The name of the module is abc.math.utils and the module is packaged as a jar file named mathutils.jar. The module contains a class named abc.utils.Main.

You want to run this class from the command line. Which of the following commands can be used?

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

Select 1 option(s):
35.

Question: Identify the correct statements regarding the following program?

package trywithresources;

import java.io.IOException;

public class Device implements AutoCloseable{
    String header = null;
    public Device(String name) throws IOException{
        header = name;
        if("D2".equals(name)) throw new IOException("Unknown");
        System.out.println(header + " Opened");        
    }

    public String read() throws IOException{
        return "";
    }
    
    public void close(){
        System.out.println("Closing device "+header);
        throw new RuntimeException("RTE while closing "+header);
    }
    
    public static void main(String[] args) throws Exception {
        try(Device d1 = new Device("D1");
            Device d2 = new Device("D2")){
            throw new Exception("test");
        }
    }
    
}

Select 1 option(s):
36.

Question: What can be inserted in the code below so that it will print {a=x, b=xy, c=y}?

Map<String, String> map1 = new HashMap<>();
map1.put("a", "x");
map1.put("b", "x");
//INSERT CODE HERE
map1.merge("b", "y", f);
map1.merge("c", "y", f);
System.out.println(map1);

Select 1 option(s):
37.

Question:

Given:

//creating Book objects using Book(String title, String author) constructor
var books = List.of(new Book("The Outsider", "Stephen King"),
        new Book("The Shining", "Stephen King" ), new Book("Uri", "India"));
Stream<Book> bkStrm = books.stream();//

long count = *INSERT CODE HERE*;
System.out.println(count);


Assuming Book class has appropriate constructor and accessor methods, which of the following statements will correctly print the number of unique authors in the list of Books?

Select 1 option(s):
38.

Question:

Given:

interface Runner {
  public void run();
}


Which of the following is/are valid lambda expression(s) that capture(s) the above interface?

Select 2 option(s):
39.

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

class ABCD{
int x = 10;
static int y = 20;
}
class MNOP extends ABCD{
int x = 30;
static int y = 40;
}
public class TestClass {
public static void main(String[] args) {
System.out.println(new MNOP().x+", "+new MNOP().y);
}
}

Select 1 option(s):

40.

Question:

Consider the following code for the main() method:

public static void main(String[] args) throws Exception{
int i = 1, j = 10;
do {
if (i++ > --j) continue;
} while (i < 5);
System.out.println("i=" + i + " j=" + j);
}

What will be the output when the above code is executed?

Select 1 option(s):
41.

Question:

Identify correct statement(s) about the following code:

public double getTotal(String file) throws IOException{
    if(!checkFile(file)) throw new IllegalArgumentException("Invalid file : " +file);
    var f = new File(file);
    var bfr = new BufferedReader(new FileReader(f));
    String line = null;
    var sum = 0.0;
    while( (line = bfr.readLine()) != null ){
        sum = sum + Double.parseDouble(line.trim());
    }
    bfr.close();
    return sum;
}

Select 1 option(s):
42.

Question:

Given:

List<Integer> ls = Arrays.asList(3,4,6,9,2,5,7);
System.out.println(ls.stream().reduce(Integer.MIN_VALUE, (a, b)->a>b?a:b)); //1
System.out.println(ls.stream().max(Integer::max).get()); //2
System.out.println(ls.stream().max(Integer::compare).get()); //3
System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4

Which of the above statements will print 9?

Select 1 option(s):
43.

Question:

Given:

class StaticTest{
void m1(){
StaticTest.m2(); //
1 m4(); // 2
StaticTest.m3(); // 3
}
static void m2(){ } // 4
void m3(){
m1(); // 5
m2(); // 6
StaticTest.m1(); // 7
}
static void m4(){ }
}

Which of the lines will fail to compile?

Select 2 option(s):
44.

Question:

Given:

public class Shape {

   Point[] vertices;

   public Shape(Point[] verts){  this.vertices = verts; }

   public Points[] getVertices(){ return vertices; }   

   public void setVertices(Point[] vertices){ this.vertices = vertices; }
}


Which actions implement Java SE security guidelines?

Select 3 option(s):
45.

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

import java.util.*;

interface Birdie {
    void fly();
}

class Dino implements Birdie {
    public void fly(){ System.out.println("Dino flies"); }
    public void eat(){ System.out.println("Dino eats");}
}

class Bino extends Dino {
    public void fly(){ System.out.println("Bino flies"); }
    public void eat(){ System.out.println("Bino eats");}
    
}

public class TestClass {
    public static void main(String[] args)    {
       List<Birdie> m = new ArrayList<>();
       m.add(new Dino());
       m.add(new Bino());
       for(Birdie b : m) {
    b.fly();
    b.eat();
       }        
    }
}

Select 1 option(s):
46.

Question: What will be a part of the output when the following code is compiled and run?

class Boo {
    int boo = 10;
    public Boo(int k){ System.out.println("In Boo k = "+k); boo = k;}
}

class BooBoo extends Boo {
     public BooBoo(int k ){ super(k); System.out.println("In BooBoo k = "+k); }
}
    
class Moo extends BooBoo implements Serializable {
    int moo = 10;
    public Moo(){ super(5); System.out.println("In Moo"); }
}
    
public class TestClass {
    
    public static void main(String[] args) throws Exception{
    
        var moo = new Moo();
        var fos = new FileOutputStream("c:\\temp\\moo1.ser");
        var os = new ObjectOutputStream(fos);
        os.writeObject(moo);
        os.close();
        var fis = new FileInputStream("c:\\temp\\moo1.ser");
        var is = new ObjectInputStream(fis);
        moo = (Moo) is.readObject();
        is.close();    
      }
}

Select 3 option(s):
47.

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 can be added to the above code so that the update is committed to the database?

Select 1 option(s):
48.

Question:

You are trying to modularize an existing application that uses a few Apache open source jar files. Some of those jar files have been modularized but some have not. Details of such jar files are as follows:

commons-beanutils-1.9.jar

commons-collections4-4.0.jar
   (Automatic-Module-Name: org.apache.commons.collections4)

commons-lang3-3.6.jar
   (Automatic-Module-Name: org.apache.commons.lang3)

commons-text-1.2.jar
   (Automatic-Module-Name: org.apache.commons.text)


What should the module-info file for your application contain?

Select 1 option(s):
49.

Question:

Given:

List<Integer> ls = Arrays.asList(10, 47, 33, 23);

int max = //INSERT code HERE
System.out.println(max); //1


Which of the following options can be inserted above so that it will print the largest number in the input stream?

Select 1 option(s):
50.

Question:

Given:

@Target(ElementType.TYPE)
public @interface DBTable {
  public String value();
  public String[] primarykey();
  public String surrogateKey() default "id";
}

Identify correct usages of the above annotation.

Select 2 option(s):
51.

Question:

Given:

class TestClass{
    public static void main(String[] args) throws IOException {
        
        final InputStream fis = new FileInputStream("c:\\temp\\test.txt");
        long l = 0;
        try(fis){
            l = fis.read();//1
        }finally{
            l = fis.read();//2
        }
        l = fis.read();//3
        System.out.println(l);//4
   }
}

What will be the output?

Select 1 option(s):
52.

Question: What will the following code print?

public class TestClass{
int x = 5;
int getX(){ return x; }
public static void main(String args[]) throws Exception{
TestClass tc = new TestClass();
tc.looper();
System.out.println(tc.x);
}
public void looper(){
var x = 0;
while( (x = getX()) != 0 ){
for(int m = 10; m>=0; m--){
x = m;
}
}
}
}

Select 1 option(s):

53.

Question:

Given the following contents of module-info.java,

module enthu.finance{
  exports com.enthu.Reports;
  requires enthu.utils;
}


Select correct statements.

Select 1 option(s):