Welcome to your Test - 1 Code - 819

Name
Email
Phone
1.

Question:

Consider the following code:

interface Flyer{ String getName(); }

class Bird implements Flyer{
    public String name;
    public Bird(String name){
        this.name = name;
    }
    public String getName(){ return name; }
}

class Eagle extends Bird {
    public Eagle(String name){
        super(name);
    }
}

public class TestClass {
    public static void main(String[] args) throws Exception {
        Flyer f = new Eagle("American Bald Eagle");
        //PRINT NAME HERE
   }
}

Which of the following lines of code will print the name of the Eagle object?

Select 3 option(s):
2.

Question:

Given:

public class Book {
   private String title;
   private String genre;
   public Book(String title, String genre){
        this.title = title; this.genre = genre;
   }

   //accessors not shown
}

and the following code:

List<Book> books = //code to create the list goes here

Comparator<Book> c1 =  (b1, b2)->b1.getGenre().compareTo(b2.getGenre()); //1
books.stream().sorted(c1.thenComparing(Book::getTitle)); //2
System.out.println(books);

Identify the correct statements.

Select 1 option(s):
3.

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

public class TestClass{
   public static void main(String args[ ] ){
      int i, j, k;
      i = j = k = 9;
      System.out.println(i);
   }
}

Select 2 option(s):
4.

Question:

Given the following method code appearing in an application that manages files on a server machine:

List<File> dir; //initialize dir somehow
public <R> List<R> executeFunction(Function<File, R> fun){
    List<R> l = new ArrayList<R>();
    for(File f : dir){
        l.add(fun.apply(f));
    }
    return l;
}

The caller of this method passes in a Function that takes a java.io.File object and performs whatever operation is need to be done on that file.  It is required that the above code must ensure that the caller only reads a file and is not able to overwrite or delete it irrespective of what level of permission the caller has. How can this be done?

Select 1 option(s):
5.

Question: What exception will be thrown out of the main method when the following program is run?

package trywithresources;

import java.io.IOException;

public class Device implements AutoCloseable{
    String header = null;
    public void open() throws IOException{
        header = "OPENED";
        System.out.println("Device Opened");
        throw new IOException("Unknown");
    }

    public String read() throws IOException{
        return "";
    }
    
    public void close(){
        System.out.println("Closing device");
        header = null;
        throw new RuntimeException("rte");
    }
    
    public static void main(String[] args) throws Exception {
        try(Device d = new Device()){
            throw new Exception("test");
        }
    }
    
}

Select 1 option(s):
6.

Question: What will the following code print when run?

import java.io.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MarkTest {

    public static void main(String[] args) {
        
        try (Reader r = new BufferedReader(new FileReader("c:\\temp\\test.txt"))) {
            if (r.markSupported()) {
                BufferedReader in = (BufferedReader) r;
                System.out.print(in.readLine());
                in.mark(100);
                System.out.print(in.readLine());
                System.out.print(in.readLine());
                in.reset();                   
                System.out.print(in.readLine());
                in.reset();                     
                System.out.println(in.readLine());
            }else{
                System.out.println("Mark Not Supported");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Assume that the file test.txt contains:
A
B
C
D
E

Select 1 option(s)
7.

Question: What changes can be made to the following code so that it will print :

Old Rating A
New Rating R


List<Character> ratings = Arrays.asList('U', 'R', 'A');
ratings.stream()
        .filter(x->x=='A')  //1
        .peek(x->System.out.println("Old Rating "+x)) //2
        .map(x->x=='A'?'R':x) //3
        .peek(x->System.out.println("New Rating "+x)); //4

Select 1 option(s):
8.

Question:

Consider the following code:

public class SubClass extends SuperClass{
int i, j, k;
public SubClass( int m, int n ) { i = m ; j = m ; } //1
public SubClass( int m ) { super(m ); } //2
}

Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?

Select 2 option(s):
9.

Question:

Given:

//Insert code here  

   public abstract void draw();
}


//Insert code here
   public void draw(){  System.out.println("in draw..."); }
}


Which of the following lines of code can be used to complete the above code?

Select2 option(s):
10.

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

Select 3 option(s):
11.

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

public class InitClass{
public static void main(String args[ ] ){
InitClass obj = new InitClass(5);
}
int m;
static int i1 = 5;
static int i2 ;
int j = 100;
int x;
public InitClass(int m){
System.out.println(i1 + " " + i2 + " " + x + " " + j + " " + m);
}
{ j = 30; i2 = 40; } // Instance Initializer
static { i1++; } // Static Initializer
}

Select 1 option(s):

12.

Question:

Given:

Stream<Integer> strm1 = Stream.of(2, 3, 5, 7, 11, 13, 17, 19); //1
Stream<Integer> strm2 = strm1.filter(i->{ return i>5 && i<15; });  //2
strm2.forEach(System.out::print); //3


Which of the following options can be used to replace line at //2 and still print the same elements of the stream?

Select 1 option(s):
13.

Question: Which of the following code fragments correctly loads a service provider that implements api.BloggerService?

Select 1 option(s):
14.

Question:

Identify the right declaration for 'box' for the following code.

import java.util.*;
class Dumper {
    // declaration for box
    
  public void dumpStuff(){
    for(List<String> l : box.values()){
       System.out.println(l);
    }
  }
}


Select 2 option(s):
15.

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

class Holder{
int value = 1;
Holder link;
public Holder(int val){ this.value = val; }
public static void main(String[] args){
final var a = new Holder(5);
var b = new Holder(10);
a.link = b;
b.link = setIt(a, b);
System.out.println(a.link.value+" "+b.link.value);
}
public static Holder setIt(final Holder x, final Holder y){
x.link = y.link;
return x;
}
}

Select 1 option(s):

16.

Question:

Consider the following classes :

interface I{
}
class A implements I{
}
class B extends A {
}
class C extends B{
}

And the following declarations:

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

Identify options that will compile and run without error.

Select 1 option(s):
17.

Question:

Given:

ConcurrentMap<String, Object> cache = new ConcurrentHashMap<>();
cache.put("111", student1);
(Assume that student1 and student2 are references to a valid objects.)

Which of the following statements are legal but will NOT modify the Map referenced by cache?

Select 1 option(s):
18.

Question: What will the following code print when run?

public class TestClass{
public static Integer wiggler(Integer x){
Integer y = x + 10;
x++;
System.out.println(x);
return y;
}
public static void main(String[] args){
Integer dataWrapper = new Integer(5);
Integer value = wiggler(dataWrapper);
System.out.println(dataWrapper+value);
}
}

Select 1 option(s):

19.

Question: Which of the following code snippets appearing in a method are valid.

Select 1 option(s):
20.

Question: Which of the following statements are correct regarding the module system of Java.

Select 1 option(s):
21.

Question:

Given:

module foo.filter{
  requires api;
  provides api.Filter with foo.DoNothingFilter;
}

Assuming that api.Filter is an interface with just a single method List<String> filter(List<String> l), which of the following is a valid implementation of foo.DoNothingFilter class?

Select 1 option(s):
22.

Question: What will the following code print?

public class BreakTest{
public static void main(String[] args){
int i = 0, j = 5;
lab1 : for( ; ; i++){
for( ; ; --j) if( i >j ) break lab1;
}
System.out.println(" i = "+i+", j = "+j);
}
}

Select 1 option(s):

23.

Question:

Given that your module named foo.bar contains the following two source files: src/foo.bar/f/b/Baz1.java and src/foo.bar/f/c/Caz1.java.

Which of the following options can be used to compile this module?

(Assume that all directory paths are relative to the current directory.)

Select 1 option(s):
24.

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

int [] [] array = {{0}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3}, {0, 1, 2, 3, 4}};
var arr1 = array[4];
System.out.println (arr1[4][1]);
System.out.println (array[4][1]);

Select 1 option(s):
25.

Question:

Given that the user account under which the following code is run does not have the permission to access a.java, what will the the following code print when run?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.NoSuchFileException;

public class IOTest {
    public static void main(String[] args) {
        try(BufferedReader bfr = new BufferedReader(new FileReader("c:\\works\\a.java"))){
            String line = null;
            while( (line = bfr.readLine()) != null){
                System.out.println(line);
            }
        }catch(NoSuchFileException|IOException|AccessDeniedException e){
            e.printStackTrace();
        }
    }
}

Select 1 option(s):
26.

Question: What will the following code print?

AtomicInteger ai = new AtomicInteger();
Stream<String> stream = Stream.of("old", "king", "cole", "was", "a", "merry", "old", "soul").parallel();
stream.filter( e->{
    ai.incrementAndGet();
    return e.contains("o");
}).allMatch(x->x.indexOf("o")>0);
        
System.out.println("AI = "+ai);

Select 1 option(s):
27.

Question:

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

Statement stmt = null;
Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
try(stmt = c.createStatement();)
{

    ResultSet rs = stmt.executeQuery("select * from STUDENT");
    while(rs.next()){
        System.out.println(rs.getString(0));
    }

}
catch(SQLException e){
    System.out.println("Exception ");
}

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

Select 1 option(s):
28.

Question:

You are the maintainer of a library packaged as bondanalytics.jar, which is used by several groups in your company. It has the following two packages that are used by other applications:
com.abc.bonds
com.abc.bonds.analytics

You want to modularize this jar with least impact to others. What will you do?

Select 1 option(s):
29.

Question:

Given:

int[][] iaa = { {1, 2}, {3, 4}, { 5, 6} };
long count = Stream.of(iaa).flatMapToInt(IntStream::of)
        .map(i->i+1).filter(i->i%2 != 0).peek(System.out::print).count();
System.out.println(count);


What will be the output?

Select 1 option(s):
30.

Question:

Given the following module-info:

module book{
   requires org.pdf;
   uses org.pdf.Print;
}


Which of the following statements are correct?

Select 2 option(s):
31.

Question: What is the result of executing the following fragment of code:

boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2){
   System.out.println("true");
} else{
   System.out.println("false");
}

Select 1 option(s):
32.

Question: What will the following code print when run?

import java.util.function.*;
class Employee{
    int age;   
}

public class TestClass{
    
   public static void main(String[] args) {
       Employee e = new Employee();
       Supplier<Employee> se = ()->{ e.age = 40; return e; }; //1
       e.age = 50;//2
       System.out.println(se.get().age); //3
   }
}

Select 1 option(s):
33.

Question:

Consider the following code:

package mypack;
public class TestOuter
{
   public static class TestInner
   {
       public void sayIt(){ System.out.println("hello"); }
   }
   public static void main(String[] args){
      //call here
   }
}

Which of the following options are correct?

Select 2 option(s):
34.

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

var numA = new Integer[]{1, null, 3}; //1
var list1 = List.of(numA); //2
var list2 = Collections.unmodifiableList(list1); //3
numA[1] = 2; //4
System.out.println(list1+" "+list2);

Select 1 option(s):
35.

Question:

Given:

  static boolean validateInput(String str){
     return INSERT CODE HERE;
  }

Which of the following expressions can be inserted in the above code so that the validateInput method will return true if and only if the input string contains non-whitespace data?

Select 1 option(s):
36.

Question:

A new Java programmer has written the following method that takes an array of ints and sums up all the integers that are less than 100.


public void processArray(int[] values){
        int sum = 0;
        int i = 0;
        try{
            while(values[i]<100){
                sum = sum +values[i];
                i++;
            }
        }
        catch(Exception e){ }
        System.out.println("sum = "+sum);
    }

Which of the following are best practices to improve this code?

Select 2 option(s):
37.

Question:

Consider the following class:

public class PortConnector{
   public PortConnector(int port) throws IOException{
   ...lot of valid code.
   }
   ...other valid code.
}


You want to write another class CleanConnector that extends from PortConnector. Which of the following statements should hold true for CleanConnector class?

Select 1 option(s):
38.

Question: Which of the following options correctly makes use of java.util.concurrent.Callable?

Select 1 option(s):
39.

Question:

Consider the following code:

import java.io.*;
public class Test
{
   public static void main(String[] args)  throws Exception
   {
      var fw = new FileWriter("text.txt");
      // fw.write("hello"); //1
      fw.close();
   }
}


Which of the following statements are correct?

Select 1 option(s):
40.

Question:

Consider the following code appearing in the same file:

class Data {
private 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 1 option(s):
41.

Question: What will the following code print when run?

        Deque<Integer> d = new ArrayDeque<>();
        d.push(1);
        d.push(2);
        d.push(3);
        System.out.println(d.pollFirst());
        System.out.println(d.poll());
        System.out.println(d.pollLast());

Select 1 option(s):
42.

Question: What will the following code print?

Map<String, Integer> map1 = new HashMap<>();
map1.put("a", 1);
map1.put("b", 1);
map1.merge("b", 1, (i1, i2)->i1+i2);
map1.merge("c", 3, (i1, i2)->i1+i2);
System.out.println(map1);

Select 1 option(s):
43.

Question:

Given:

class Base{
   public <T> Collection<T> transform(Collection<T> list)
   {
      return new ArrayList<T>();
   }
}

class Derived extends Base{
   //public <T> Collection<T> transform(Collection<T> list) { return new HashSet<String>(); }; //1
   
   //public <T> Collection<T> transform(Stream<T> list) { return new HashSet<T>();}; //2
   
   //public <T> List<T> transform(Collection<T> list) { return new ArrayList<T>(); }; //3
   
   //public <X> Collection<X> transform(Collection<X> list) { return new HashSet<X>();}; //4
    
   //public Collection<CharSequence> transform(Collection<CharSequence> list) { return new HashSet<CharSequence>();}; //5

}


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

Select 2 option(s):
44.

Question: Which of these for statements are valid when present in a method?

1. for (var i=5; i=0; i--) { }
2. var j=5; for(int i=0, j+=5; i<j ; i++) { j--; }
3. int i, j; for (j=10; i<j; j--) { i += 2; }
4. var i=10; for ( ; i>0 ; i--) { }
5. for (int i=0, j=10; i<j; i++, --j) {;}
6. for (var i=0, j=10; i<j; i++, --j) {;}

Select 1 option(s):

45.

Question: What will the following code fragment print?

        Path p1 = Paths.get("photos/goa");
        Path p2 = Paths.get("/index.html");
        Path p3 = p1.relativize(p2);
        System.out.println(p3);

Select 1 option(s):
46.

Question:

Given:

public class CrazyMath {
    public static void main(String[] args) {
        int x = 10, y = 20;
        int dx, dy;
        try{
            dx = x % 5;
            dy =  y/dx;
        }catch(ArithmeticException ae){
            System.out.println("Caught AE");
            dx = 2;
            dy = y/dx;
        }
        x = x/dx;
        y = y/dy;
        System.out.println(dx+" "+dy);
        System.out.println(x+" "+y);
        
    }
}

What is the output?

Select 1 option(s):
47.

Question:

Given the following code:

class TestClass{
   public static void main(String args[]){
      int time = 100;
      java.sql.Timestamp ts = new java.sql.Timestamp(time);
      java.util.Date d = new java.util.Date();
      ts = new java.sql.Timestamp(d.getTime());
      System.out.println(ts);
   }
}

and the following commands:

javac TestClass.java
jdeps -summary TestClass.java

What will be the output?

Select 1 option(s):
48.

Question:

Your application is packaged in myapp.jar and depends on a jar named datalayer.jar, which in turn depends on mysql-connector-java-8.0.11.jar.

You have modularized myapp without waiting for datalayer and mysql driver to be modularized and the following are the contents of myapp's module-info:

module abc.myapp{
  requires datalayer;
}


Assming that com.abc.myapp.Main is the main class of your application that you want to execute, which of the following commands will successfully launch your application?

Select 3 option(s):
49.

Question:

Given:

interface AmazingInterface{
    String value = "amazing";
    void amazingMethod(String arg);
}

abstract class AmazingClass implements AmazingInterface{
    static String value = "awesome";
    abstract void amazingMethod(String arg1, String arg2);
}


public class Awesome extends AmazingClass implements AmazingInterface {
    public void amazingMethod(String arg1){ }
    public void amazingMethod(String arg1, String arg2){ }
   
    public  void main(String[] args){
        AmazingInterface ai = new Awesome();

        //INSERT CODE HERE
    }

}        


Identify the options that will cause compilation failure.

Select 3 option(s):
50.

Question:

Given:

enum Title
{
    MR("Mr."), MS1("Ms."), MS2("Ms.");
    private String title;
    private Title(String s){
    title = s;
    }  
}

public class TestClass{
    public static void main(String[] args) {
        var ts = new TreeSet<Title>();
        ts.add(Title.MS2);
        ts.add(Title.MR);
        ts.add(Title.MS1);
        for(Title t :  ts){
            System.out.println(t);
        }
    }
}

What will be the output when TestClass is compiled and run?

Select 1 option(s):
51.

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

class Data {
private int x = 0;
private String y = "Y";
public Data(int k){
this.x = k;
}
public Data(String k){
this.y = k;
}
public void showMe(){
System.out.println(x+y);
}
}
public class TestClass {
public static void main(String[] args) throws Exception {
new Data(10).showMe();
new Data("Z").showMe();
}
}

Select 1 option(s):

52.

Question: Which of the following are benefits of the Java module system?

Select 1 option(s):