Welcome to your Test - 4 Code - 819

Name
Email
Phone
1.

Question: What will be the result of compilation and execution of the following code ?

DoubleStream is = DoubleStream.of(0, 2, 4); //1
double sum = is.filter( i->i%2 != 0 ).sum(); //2
System.out.println(sum); //3

Select 1 option(s):
2.

Question: What will the following code fragment print?

Path p1 = Paths.get("photos\\..\\beaches\\.\\calangute\\a.txt");
Path p2 = p1.normalize();
Path p3 = p1.relativize(p2);
Path p4 = p2.relativize(p1);

System.out.println(
    p1.getNameCount()+" "+p2.getNameCount()+" "+
    p3.getNameCount()+" "+p4.getNameCount());

Select 1 option(s):
3.

Question:

Given:

    public static void main(String[] args) {
        String str = "10";
        int iVal = 0;
        Double dVal = 0.0;
        try{
            iVal = Integer.parseInt(str, 2);  //1
            if((dVal = Double.parseDouble(str)) == iVal){ //2
                System.out.println("Equal");
            }
        }catch(NumberFormatException e){
            System.out.println("Exception in parsing");
        }
        System.out.println(iVal+" "+dVal);
        
    }

What is the result?

Select 1 option(s):
4.

Question: Which statements about the following code are correct?

interface House{
  public default String getAddress(){
     return "101 Main Str";
  }
}

interface Bungalow extends House{
  public default String getAddress(){
     return "101 Smart Str";
  }
}

class MyHouse implements Bungalow, House{

}

public class TestClass {

  public static void main(String[] args) {
    House ci = new MyHouse();  //1
    System.out.println(ci.getAddress()); //2
  }
}

Select 1 option(s):
5.

Question:

Given:

public @interface MyArtifact {
    int id() default 0;
    String name();
}


Which of the following are correct usages of the above annotation?

Select 2 option(s):
6.

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

public class TestClass{
public static void main(String args[]){
int x = 0;
labelA: for (var i=10; i<0; i--){
var j = 0;
labelB:
while (j < 10){
if (j > i) break labelB;
if (i == j){
x++;
continue labelA;
}
j++;
}
x--;
}
System.out.println(x);
}
}

Select 1 option(s):

7.

Question:

Given that Book is a valid class with appropriate constructor and getPrice and getTitle methods that returns a double and a String respectively, consider the following code:

List<List<Book>> books = Arrays.asList(
        Arrays.asList(
                new Book("Windmills of the Gods", 7.0),
                new Book("Tell me your dreams",9.0) ),
        Arrays.asList(
                new Book("There is a hippy on the highway", 5.0),
                new Book("Easy come easy go", 5.0)) );

double d = books.stream()
        INSERT CODE HERE //1
        INSERT CODE HERE //2
        .sum();
System.out.println(d);

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

Select 1 option(s):
8.

Question:

Consider the following method which is called with an argument of 7:

public void method1(int i, double d){
int j = (i*30 - 2)/100;
POINT1 : for(;j<10; j++){
var flag = false;
while(!flag){
if(d > 0.5) break POINT1;
}
}
while(j>0){
System.out.println(j--);
if(j == 4) break POINT1;
}
}

What will it print?

Select 1 option(s):
9.

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

class Test{
public static void main(String args[]){
int c = 0;
A: for(var i = 0; i < 2; i++){
B: for(var j = 0; j < 2; j++){
C: for(var k = 0; k < 3; k++){
c++;
if(k>j) break;
}
}
}
System.out.println(c);
}
}

Select 1 option(s):

10.

Question: What will be the output?

        var ca = new char[]{'a', 'b', 'c', 'd'};
        var i = 0;
        for(var c : ca){
            switch(c){
                case 'a' : i++;
                case 'b' : ++i;
                case 'c'|'d' : i++;
            }
        }
        System.out.println("i = "+i);

Select 1 option(s):
11.

Question:

Consider the following code:

    public static void findFiles() throws Exception{
        Path dir = Paths.get("c:\\temp");
        
        //INSERT CODE HERE
           for(Path p : ds){
              System.out.println(p);
           }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }    


What should be inserted in the above code so that it will print all the files with extension gif and jpeg?

Select 1 option(s):
12.

Question: Which of the following declarations is/are valid inside a static method:

1. bool b = null;

2. boolean b = 1;

3. boolean b = true|false;

4. bool b = (10<11);

5. boolean b = true||false;

6. var  b = 10>11;

Select 1 option(s):
13.

Question: Given the following interface definition, which definitions are valid?

interface I1{
   void setValue(String s);
   String getValue();
}


Select 2 option(s):
14.

Question: What will the following code print when run?

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathTest {
    static Path p1 = Paths.get("c:\\a\\b\\c");
    
    public static String getValue(){
        String x = p1.getName(1).toString();
        String y = p1.subpath(1,2).toString();
        return x+" : "+y;
    }
    
    public static void main(String[] args) {
        System.out.println(getValue());
    }
}

Select 1 option(s):
15.

Question: What will be the output if you run the following program?

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

Select 1 option(s):

16.

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

class X{
    public X(){
        System.out.println("In X");
    }
}

class Y extends X{
    public Y(){
        super();
        System.out.println("In Y");
    }
}

class Z extends Y{
    public Z(){
        System.out.println("In Z");
    }
}

public class Test {

    public static void main(String[] args) {
        Y y = new Z();
    }
}

Select 1 option(s):
17.

Question:

Given:

class A{
    private String s;
    //1
    public boolean equals(A a){ //override the equals methods
        return this.s != null && this.s.equals(a.s);
    }
}


Select 2 option(s):
18.

Question:

Consider the following code:

public class TestClass{
public void method(Object o){
System.out.println("Object Version");
}
public void method(java.io.FileNotFoundException s){
System.out.println("java.io.FileNotFoundException Version");
}
public void method(java.io.IOException s){
System.out.println("IOException Version");
}
public static void main(String args[]){
TestClass tc = new TestClass();
tc.method(null);
}
}

What would be the output when the above program is compiled and run? (Assume that FileNotFoundException is a subclass of IOException, which in turn is a subclass of Exception)

Select 1 option(s):
19.

Question: What will the following code print when run?

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathTest {
    static Path p1 = Paths.get("c:\\main\\project\\Starter.java");
    
    public static String getRoot(){
        String root = p1.getRoot().toString();
        return root;
    }
    
    public static void main(String[] args) {
        System.out.println(getRoot());
    }
}

Select 1 option(s):
20.

Question:

Given:

import java.util.*;
class Student{ }  

public class TestClass {

   var students = new ArrayList<Student>(); //1

   public static void main(String[] args)  {

       var student  = new Student(); //2

       var allStudents = new ArrayList<>();  //3

       allStudents.add(student); //4
       
       for(var s : allStudents){ //5
          System.out.println(s);
       }
       Student s2 = allStudents.get(0); //6

       var var = "what?"; //7
   }

}

Which lines will cause compilation error?

Select 2 option(s):
21.

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

class StringWrapper {
   private String theVal;
   public StringWrapper(String str){ this.theVal = str; }
}
public class Tester{
    public static void main(String[] args) {
        StringWrapper sw = new StringWrapper("How are you?");
        StringBuilder sb = new StringBuilder("How are you?");
        System.out.println("Hello, "+sw);
        System.out.println("Hello, "+sb);
   }
}

Select 1 option(s):
22.

Question:

Given :

import java.util.*;
class MyStringComparator implements Comparator
{
    public int compare(Object o1, Object o2)
    {
      int s1 = ((String) o1).length();
      int s2 = ((String) o2).length();
      return s1 - s2;
    }
}

and

static String[] sa = { "d", "bbb", "aaaa" };

Select correct statements.

Select 2 option(s):
23.

Question: What can be inserted in the code below so that it will print UP UP UP?

public class Speak {
    public static void main(String[] args) {
        Speak s = new GoodSpeak();

        INSERT CODE HERE

    }
}
class GoodSpeak extends Speak implements Tone{
    public void up(){
        System.out.println("UP UP UP");
    }
}
interface Tone{
    void up();
}

Select 2 option(s):
24.

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 int getCacheSize(){
        return cal.size();
    }
}

Given that one thread calls the addData method on an instance of the above class with a List containing 10 Strings and another thread calls the getCacheSize method on the same instance at the same time, which of the following options are correct?

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

Select 1 option(s):
25.

Question:

Consider the following code:

class A{
A() { print(); }
void print() { System.out.println("A"); }
}
class B extends A{
int i = 4;
public static void main(String[] args){
A a = new B();
a.print();
}
void print() { System.out.println(i);
}
}

What will be the output when class B is run ?

Select 1 option(s):
26.

Question:

Consider the following code:

        //INSERT CODE HERE
        a[0][0] = 1;
        a[0][1] = 2;
        

        a[1][0] = 3;
        a[1][1] = 4;
        a[1][2] = 5;
        a[1][3] = 6;

What can be inserted independently in the above code so that it will compile and run without any error or exception?

Select 2 option(s):
27.

Question: What will the following code print when run?

    String[] sa = { "charlie", "bob", "andy", "dave" };
    var ls = new ArrayList<String>(Arrays.asList(sa));
    ls.sort((var a, var b) -> a.compareTo(b));
    System.out.println(sa[0]+" "+ls.get(0));

Select 1 option(s):
28.

Question: You are creating an acme.accounting module that depends on acme.math module and makes its com.acme.accounting package available to all other modules. Which of the following files correctly defines this module?

Select 1 option(s):
29.

Question:

Given:

List<Double> dList = Arrays.asList(10.0, 12.0);
dList.forEach(x->{ x = x + 10; });
dList.forEach(x->System.out.println(x));

What will it print when compiled and run?

Select 1 option(s):
30.

Question:

A programmer is using the following class for wrapping objects and passing it around to multiple threads. Which of the given statements regarding this class are correct?

public class DataObjectWrapper
{
   private final Object obj;

   public DataObjectWrapper(Object pObj){ obj = pObj; }

   public Object getObject() { return obj; }
}


Select 1 option(s):
31.

Question:

Given the following code:

import java.util.Arrays;
import java.util.List;
public class TestClass {
    public static void main(String[] args) {
        List<String> al = Arrays.asList("aa", "aaa", "b", "cc", "ccc", "ddd", "a");
        //INSERT CODE HERE
        System.out.println(count);
    }
}

Which of the following options will correctly print the number of elements that will come before the string "c" if the list were sorted alphabetically?

Select 1 option(s):
32.

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

List<String> countries = List.of("US", "UK", "India", "Argentina");
List<String> capitals = List.of("Washington DC", "London", "New Delhi" );
Stream<String> is = IntStream
        .range(0, Math.min(countries.size(), capitals.size()))
        .mapToObj(i->countries.get(i)+" "+capitals.get(i));
is.forEach(System.out::println);

Select 1 option(s):
33.

Question:

Identify the correct statements about the following code -

IntStream is1 = IntStream.of(1, 3, 5);  //1
OptionalDouble x = is1.filter(i->i%2 == 0).average(); //2
System.out.println(x); //3

IntStream is2 = IntStream.of(2, 4, 6); //4
int y = is2.filter( i->i%2 != 0 ).sum(); //5
System.out.println(y); //6

Select 1 option(s):
34.

Question:

A programmer has written the following code for Bond class. The programmer intends this class to be immutable.
class Bond {    private final String isin;
    private final double coupon;
    private final java.util.Date maturityDate;
    private Bond(String isin, double coupon, java.util.Date matDate){
        this.isin = isin; this.coupon = coupon; this.maturityDate = matDate;
    }

    public static Bond build(String isin, double coupon, java.util.Date matDate){
        return new Bond(isin, coupon, matDate);
    }
    
    public String getIsin(){ return isin; }
    public double getCoupon(){ return coupon; }
    public java.util.Date getMaturityDate(){ return maturityDate; }
}


Identify correct statement about the above code.

Select 2 option(s):
35.

Question:

Given the following code:

public static void main(String[] args) {
        LocalDate d1 = LocalDate.now();
        d1.plusDays(10);
        LocalDate d2 = d1.minusWeeks(1);
        d1 = null;
        LocalDate d3 = LocalDate.now().plusYears(3).minusMonths(4);
        d2.plusWeeks(5);
        d1 = d2;
}

How many objects are created/instantiated by the given code?

Select 1 option(s):
36.

Question:

Identify correct statement(s) regarding the benefit of using PreparedStatement over Statement.

Select 1 option(s):
37.

Question:

Consider the program shown in exhibit and select the right option(s).

public class TestClass
{
  static StringBuffer sb1 = new StringBuffer();
  static StringBuffer sb2 = new StringBuffer();
  public static void main(String[] args)
  {
      new Thread
      (
         new Runnable()
         {
            public void run()
            {
                synchronized(sb1)
                {
                    sb1.append("X");
                    synchronized(sb2)
                    {
                      sb2.append("Y");
                    }
                }
                System.out.println(sb1);
            }
         }
      ).start();
      new Thread
      (
         new Runnable()
         {
            public void run()
            {
                synchronized(sb2)
                {
                    sb2.append("Y");
                    synchronized(sb1)
                    {
                      sb1.append("X");
                    }
                }
               System.out.println(sb2);
            }
         }
      ).start();
  }
}


Select 1 option(s):
38.

Question: Which of the given option when inserted in the code below will make the code print true?

List<String> values = Arrays.asList("Alpha A", "Alpha B", "Alpha C");
//INSERT CODE HERE
System.out.println(flag);

Select 1 option(s):
39.

Question:

Given that listOfWords points to a List<String> of words, which of the following code snippets will create a list of two letter words?

Select 3 option(s):
40.

Question:

Given:

public class Counter{
    //1
    public  void increment(){
       //2
    }
    //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 an instance of this class and call its increment method. What should be inserted at //1 and //2?

Select 1 option(s):
41.

Question: Which of the following code fragments will you use to create an ExecutorService?

Select 2 option(s):
42.

Question: What will the following code print when run?

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathTest {
    static Path p1 = Paths.get("c:\\main\\project\\Starter.java");
    
    public static String getData(){
        String data = p1.getName(0).toString();
        return data;
    }
    
    public static void main(String[] args) {
        System.out.println(getData());
    }
}

Select 1 option(s):
43.

Question:

Given:

public class Student {

    public static enum Grade{ A, B , C, D, F}   

    private String name;
    private Grade grade;
    public Student(String name, Grade grade){
        this.name = name;
        this.grade = grade;
    }
    public String toString(){
        return name+":"+grade;
    }
    //getters and setters not shown
}

What can be inserted in the code below so that it will print:
{C=[S3], A=[S1, S2]}


List<Student> ls = Arrays.asList(new Student("S1", Student.Grade.A), new Student("S2", Student.Grade.A), new Student("S3", Student.Grade.C));
//INSERT CODE HERE
System.out.println(grouping);


Select 1 option(s):
44.

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

interface Boiler{
    public void boil();
    private static void log(String msg){ //1
       System.out.println(msg);
    }
    public static void shutdown(){
        log("shutting down");
    }
}
interface Vaporizer extends Boiler{  
    public default void vaporize(){
        boil();
        System.out.println("Vaporized!");
    }
}
    
public class Reactor implements Vaporizer{
    public void boil() {
        System.out.println("Boiling...");
    }

    public static void main(String[] args) {
        Vaporizer v =  new Reactor(); //2
        v.vaporize(); //3
        v.shutdown(); //4
    }
}

Select 1 option(s):
45.

Question:

Given:

//assume appropriate imports
public class Calculator{
    public static void main(String[] args) {
        double principle = 100;
        int interestrate = 5;
        double amount = compute(principle, x->x*interestrate);
    }
    
    INSERT CODE HERE
}

Which of the following methods can be inserted in the above code so that it will compile and run without any error/exception?

Select 2 option(s):
46.

Question: What changes, when applied independent of each other, will enable the following code to compile?

//assume appropriate import statements
class TestClass{
   public double process(double payment, int rate)
   {
      double defaultrate = 0.10;        //1
      if(rate>10) defaultrate = rate;  //2
      class Implement{
         public int apply(double data){
             Function<Integer, Integer> f = x->x+(int)(x*defaultrate);  //3
             return f.apply((int)data); //4
         }
      }
      Implement i = new Implement();
      return i.apply(payment);
   }
}

Select 2 option(s):
47.

Question:

Consider the following code:

public class TestClass {
    
    public static void doStuff() throws Exception{
        System.out.println("Doing stuff...");
        if(Math.random()>0.4){
            throw new Exception("Too high!");
        }
        System.out.println("Done stuff.");
    }
    
    public static void main(String[] args) throws Exception {
        doStuff();
        System.out.println("Over");    
    }
}


Which of the following are possible outputs when the above program is compiled and run?

(Assume that Math.random() returns a double between 0.0 and 1.0 not including 1.0.
Further assume that there is no mistake in the line numbers printed in the output shown in the options.)

Select 2 option(s):
48.

Question:

Given:

//creating Book objects using Book(String title, String author) constructor
var books = List.of(new Book("Where the Crawdads Sing", "Dalia Owens" ), new Book("The Outsider", "Stephen King"),
        new Book("Elevation", "Stephen King"), new Book("Coffin from Hong Kong", "James Hadley Chase") );
Stream<Book> bkStrm = books.stream();

*INSERT CODE HERE*

Assuming Book class has appropriate constructor and accessor methods, which of the following lines of code will get you the first book written by Stephen King from the list of of books?

Select 1 option(s):
49.

Question: What will the following code snippet print?

        List s1 = new ArrayList( );
        try{
            while(true){
                s1.add("sdfa");
            }
        }catch(RuntimeException e){
            e.printStackTrace();
        }
        System.out.println(s1.size());

Select 1 option(s):
50.

Question: 

Given the following code:

interface Device {
   public abstract void switchOn();
}

abstract class Router /* LOCATION 1 */ {
   /* LOCATION 2 */
   public void switchOn(){    }

   public abstract void reset();
}

class MimoRouter extends Router implements Device{
   /* LOCATION 3 */
}

Which code, when inserted at one or more marked locations, would allow classes Router and MimoRouter to compile?
(Consider each option as independent of other options.)

Select 2 option(s):
51.

Question: 

Consider the following method exposed by a utility class:

public static String getOptions(final String propName) {
                return AccessController.doPrivileged(
                    new PrivilegedAction<String>() {
                        public String run() {
                            return System.getProperty(propName);
                        }
                    }
                );
            }

It has been decided to give appropriate permission in the security file for this code. Identify correct statements.

Select 2 option(s):
52.

Question: Which reviewing the code for a class that is used by your cloud customers, you notice the following code in one of the methods:

public void invokeService(String srvname, String host, int port){
     ...
     Socket socket = null;
     try{
        AccessController.doPrivileged( ( PrivilegedExceptionAction<Void> ) ()->
               socket = new Socket(host, port);
               return null;
     }
     ...
}


You also notice the following permission that is given to this code in the security policy file:

permission java.io.SocketPermission "*", "connect";

What security vulnerability does this expose to your cloud customer's code?

Select 1 option(s):