Welcome to your Test - 3 Unique Test Code - 819

Name
Email
Phone
1.

Question:

Given that SomeException is a checked exception, consider the following code:

//in file A.java
public class A{
   protected void m() throws SomeException{}
}

//in file B.java
public class B extends A{
   public void m(){ }
}

//in file TestClass.java
public class TestClass{
   public static void main(String[] args){
      //insert code here. //1
   }
}

Which of the following options can be inserted at //1 without resulting in any compilation or runtime errors?

Select 1 option(s):
2.

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

var b1 = false;
var b2  = false;
if (b2 = b1 != b2){
   System.out.println("true");
} else{
   System.out.println("false");
}

Select 1 option(s):
3.

Question:

Given:

class Base{
   public  <T> Collection<T> getCollection(T t, Integer n)
   {
      return new ArrayList<T>();
   }
}

class Derived extends Base{
    
   //public  <T> List<T> getCollection(T t, Integer m) { return new ArrayList<T>(); }; //1

   //public  <T> Stream<T> getCollection(T t, Integer m) { return new ArrayList<T>(); }; //2
   
   //public  <T> void getCollection(T t, Integer m) { return new ArrayList<T>(); }; //3
   
   //public  <T> List<T> getCollection(String t, Integer m) { return new ArrayList<T>(); }; //4
}


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

Select 1 option(s):
4.

Question: Which of the following statements are valid ?

Select 3 option(s):
5.

Question: Which of the following are correct about "encapsulation"?

Select 2 option(s):
6.

Question:

An application program collects a bunch of tasks in an list and submits this list to a task execution service. This service keeps running in the background as a system process and executes the tasks by launching a thread for the list of tasks. The following code shows how the execution service processes the tasks:

public class BackgroundExecutionService {

    public void submitTask(final List<Task> tasks, final AccessControlContext acc){

new Thread(){ public void run(){

        AccessController.doPrivileged(
            new PrivilegedAction<Void>() {
                public Void run() {
                     for(Task t : tasks){

                           // process task t

                     }
                }
            }, acc);

}.start();

    ...other irrelevant code

    }


Identify correct statements.

Select 1 option(s):
7.

Question:

Consider the following code:

public class TestClass {
  
    //define tester method here
    
    public static void main(String[] args) throws Exception {
        var tc = new TestClass();
        while(tc.tester()){
            System.out.println("running...");
        }
    }
}


Which of the following options would be a valid implementation of tester() method?

Select 2 option(s):
8.

Question: Which of the following are good practices from security perspective?

Select 3 option(s):
9.

Question: What will the following code print when run?

class A{
String value = "test";
A(String val){
this.value = val;
}
}
public class TestClass {
public static void main(String[] args) throws Exception {
new A("new test").print();
}
}

Select 1 option(s):

10.

Question: What will the following code print?

DateTimeFormatter df = DateTimeFormatter.ofPattern("eeee");
LocalDate d = LocalDate.of(2000, 1, 1); //assume that it was Saturday on this date
System.out.println(df.format(d));

Select 1 option(s):
11.

Question:

Given:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DebugInfo {
    String value();
    String[] params();
    String date();
    int depth();
}

Which of the following options correctly uses the above annotation?

Select 1 option(s):
12.

Question: What will the following code print if file test1.txt exists but test2.txt doesn't exist?

public class FileCopier {
    
    public static void copy1(Path p1, Path p2) throws Exception {
        Files.copy(p1, p2, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
    }
    
    
    public static void main(String[] args) throws Exception {
        var p1 = Paths.get("c:\\temp\\test1.txt");
        var p2  = Paths.get("c:\\temp\\test2.txt");
        copy1(p1, p2);
        if(Files.isSameFile(p1, p2)){
            System.out.println("file copied");
        }else{
            System.out.println("unable to copy file");
        }
    }
}


Select 1 option(s):
13.

Question: Which of the following options will print 10?

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 {
    public static void main(String[] args) {
        var p1 = Paths.get("c:\\code\\java\\PathTest.java");
        System.out.println(p1.getName(3).toString());
    }
}

Select 1 option(s):
15.

Question:

Assuming that a file named "a.java" exists in c:\pathtest\ as well as in c:\pathtest\dir2, what will happen when the following code is compiled and run?

//imports not shown
public class MoveTest {

    public static void main(String[] args) throws IOException {
        Path p1 = Paths.get("c:\\pathtest\\a.java");
        Path p2 = Paths.get("c:\\pathtest\\dir2\\a.java");
        Files.move(p1, p2, StandardCopyOption.ATOMIC_MOVE);
        Files.delete(p1);
        System.out.println(p1.toFile().exists()+" "+
                                      p2.toFile().exists());
    }
}


Select 1 option(s):
16.

Question:

Consider the following class :

public class Parser{
public static void main( String[] args){
try{
int i = 0;
i = Integer.parseInt( args[0] );
}
catch(NumberFormatException e){
System.out.println("Problem in " + i );
}
}
}

What will happen if it is run with the following command line:

java Parser one

Select 1 option(s):

17.

Question:

Consider the following code:


class A
{
}
public class TestClass
{
   public class A
   {
       public void m() { }
   }
   class B extends A
   {
       B(){ m(); }
   }
   public static void main(String args[])
   {
      new TestClass().new A() {   public void m() { } }; //1
      var tc = new TestClass();
      //insert call to A's m() here
   }
}


Select 4 option(s):
18.

Question:

Given:

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


What can be inserted in the above code?

Select 3 option(s):
19.

Question:

Given:

public class Data implements Serializable{
   int id;  

   transient LocalDate ld;  

   private void readObject(ObjectInputStream stream)
         throws IOException, ClassNotFoundException {

         stream.defaultReadObject();
      
         ld = LocalDate.of(2020,1,1);
   }
}

Identify the correct statements about the above code.

Select 1 option(s):
20.

Question: What will be the contents of d at the end of the following code?

        Deque<Integer> d = new ArrayDeque<>();
        d.add(1);
        d.push(2);
        d.pop();
        d.offerFirst(3);        
        d.remove();
        System.out.println(d);

Select 1 option(s):
21.

Question:

Given:

abstract class Vehicle{ }
interface Drivable{ }
class Car extends Vehicle implements Drivable{ }
class SUV extends Car { }

Which of the following options will fail to compile?

Select 1 option(s):
22.

Question:

Consider the following code:

static void printElements(List<String>... la) {  //1
   for(List<String> l : la){ //2
       System.out.println(l);
   }
}


Select 1 option(s):
23.

Question:

Consider the following code snippet:

void m1() throws Exception{
try{
// line1
}
catch (IOException e){
throw new SQLException();
}
catch(SQLException e){
throw new InstantiationException();
}
finally{ throw new CloneNotSupportedException(); // CloneNotSupportedException is a checked exception.
}
}

Which of the following statements are true?

Select 2 option(s):
24.

Question: What will the following code print when run?

public class TestClass {
    public static void main(String[] args) throws Exception {
        var sa = new String[]{"a", "b", "c"};
        for(String s :  sa){
            if("b".equals(s)) continue;
            System.out.println(s);
            if("b".equals(s)) break;
            System.out.println(s+" again");
        }
    }
}


Select 1 option(s):
25.

Question:

Given that Book is a valid class with appropriate constructor and getPrice method that returns a double, what can be inserted at //1 so that it will sum the price of all the books that are priced greater than 5.0?

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

//1 INSERT CODE HERE

System.out.println(sum);


Select 2 option(s):
26.

Question:

You are creating an inventory module. This module contains a com.inventory.Queries class that uses a com.acme.DB class. This com.acme.DB class is contained in acme.db module. Which of the following files correctly defines the inventory module?

Select 1 option(s):
27.

Question: What will the following code snippet print?

Stream<Integer> strm1 = Stream.of(2, 3, 5, 7, 11, 13);
double av = strm1
   .filter(x->{ if(x>10)return true; else return false;}) //1
   .peek() //2
   .collect(Collectors.averagingInt(y->y)); //3
System.out.println(av);

Select 1 option(s):
28.

Question:

Given:

public class FastDataProcessor{
   public static int[] key = {1, 2, 3, 4, 5};

   public native byte[]  transform(byte[] input, int start, int end);

}


Identify correct statements from secure coding guidelines perspective about the above code?

Select 1 option(s):
29.

Question:

You are given two modules named enthu.training and  enthu.course packaged in training.jar and course.jar. The training module depends on the course module. You want to run a class named com.enthu.training.StartGUI contained in enthu.training module.

Which of the following commands can be used?

Select 1 option(s):
30.

Question:

Given:

public class PlaceHolder<K, V> {
    private K k;
    private V v;
    public PlaceHolder(K k, V v){
        this.k = k;
        this.v = v;
    }
    public K getK() { return k; }
    public static <X> PlaceHolder<X, X> getDuplicateHolder(X x){
        return new PlaceHolder<X, X>(x, x);
    }
    public static void main(String[] args) {
      PlaceHolder<String, String> ph1 = PlaceHolder.getDuplicateHolder("b"); //1
      PlaceHolder<String, String> ph2 = PlaceHolder<String>.getDuplicateHolder("b"); //2
      PlaceHolder<String, String> ph3 = PlaceHolder<>.getDuplicateHolder("b"); //3
      PlaceHolder<> ph4 = new PlaceHolder<String, String>("a", "b"); //4
      PlaceHolder<?, ?> ph5 = new PlaceHolder(10, 10); //5
      
    }
}


Which lines will fail compilation?

Select 2 option(s):
31.

Question: What will the following code print when run?

import java.util.function.Predicate;
class Employee{
    int age;   //1
}

public class TestClass{

   public static boolean validateEmployee(Employee e, Predicate<Employee> p){
       return p.test(e);
   }
    
   public static void main(String[] args) {
       Employee e = new Employee(); //2
       System.out.println(validateEmployee(e, e->e.age<10000)); //3
   }
}

Select 1 option(s):
32.

Question:

Consider the following class :

class Test{
public static void main(String[] args){
for (var i = 0; i < 10; i++) System.out.print(i + " "); //1
for (var i = 10; i > 0; i--) System.out.print(i + " "); //2
var i = 20; //3
System.out.print(i + " "); //4
}
}

Which of the following statements are true?

Select 4 option(s):
33.

Question:

Given the following code, which statements can be placed at the indicated position without causing compile and run time errors?

public class Test{
   int i1;
   static int i2;
   public void method1(){
      int i;
      // ... insert statements here
   }
}


Select 3 option(s):
34.

Question: Which of the following is/are valid instantiations and initializations of a multi dimensional array?

Select 2 option(s):
35.

Question: What will be the contents of s1 and s2 at the time of the println statement in the main method of the following program?

import java.util.*;
public class TestClass{
public static void main(String args[]){
Stack s1 = new Stack ();
Stack s2 = new Stack ();
processStacks (s1, s2);
System.out.println (s1 + " "+ s2);
}
public static void processStacks(Stack x1, Stack x2){
x1.push (new Integer ("100")); //assume that the method push adds the passed object to the stack.
x2 = x1;
}
}

Note:[] is used in the options below to denote each element of the Stack.

Select 1 option(s):
36.

Question: What will the following program print?

class Test{
public static void main(String[] args){
var i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}

Select 1 option(s):

37.

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

//in file Test.java
class E1 extends Exception{ }
class E2 extends E1 { }
class Test{
   public static void main(String[] args){
      try{
         throw new E2();
      }
      catch(E1 e){
         System.out.println("E1");
      }
      catch(Exception e){
         System.out.println("E");
      }
      finally{
         System.out.println("Finally");
      }
   }
}


Select 1 option(s):
38.

Question: Which of the following illustrates a good coding practice given that the database connection is to be reused?

Select 1 option(s):
39.

Question: What will the following code print when run?

public class Noobs {
    public void m(int a){
        System.out.println("In int ");
    }
    public void m(char c){
        System.out.println("In char ");
    }
    public static void main(String[] args) {
        Noobs n = new Noobs();
        int a = 'a';
        char c = 6;
        n.m(a);
        n.m(c);
    }
}

Select 1 option(s):
40.

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

var strList1 = new ArrayList<String>();
strList1.add("A");
var strList2 = Collections.unmodifiableList(strList1);
strList1.add(null);
System.out.println(strList1+" "+strList2);
strList2.add(null);
System.out.println(strList1+" "+strList2);

Select 2 option(s):
41.

Question:

Given:

Map<String , List<? extends CharSequence>> stateCitiesMap = new HashMap<String, List<? extends CharSequence>>();

Which of the following options correctly achieves the same declaration using type inference?

Select 2 option(s):
42.

Question:

Given:

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


and the following declarations:

var bL = new ArrayList<Booby>();
var tL = new ArrayList<Tooby>();

List<? extends Booby> bV = //code for initialization
List<? super Dooby> dV = //code for initialization


Which of the following statements are valid?

Select 4 option(s):
43.

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

class Base{
   public Object getValue(){ return new Object(); } //1
}

class Base2 extends Base{
   public String getValue(){ return "hello"; } //2
}

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

Select 1 option(s):
44.

Question: Which of the following statements will compile without any error?

Select 3 option(s):
45.

Question:

Given:

class OverloadingTest{
void m1(int x){
System.out.println("m1 int");
}
void m1(double x){
System.out.println("m1 double");
}
void m1(String x){
System.out.println("m1 String");
}
}
public class TestClass {
public static void main(String[] args) throws Exception {
OverloadingTest ot = new OverloadingTest();
ot.m1(1.0);
}
}

What will be the output?

Select 1 option(s):
46.

Question: Which of the following lines will cause the compilation to fail??

public enum EnumA{ A, AA, AAA};  //1

public class TestClass //2
{
   public enum EnumB{ B, BB, BBB }; //3
   public static enum EnumC{ C, CC, CCC }; //4
   public TestClass()
   {
       enum EnumD{ D, DD, DDD } //5    
   }

   public void methodX()
   {
      public enum EnumE{ E, EE, EEE } //6
   }

   public static void main(String[] args) //7
   {
       enum EnumF{ F, FF, FFF}; //8
   }
}

Select 4 option(s):
47.

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

class SomeClass{
   public static void main(String args[]){
      var size = 10;
      var arr = new int[size];
      for (var i = 0 ; i < size ; ++i) System.out.println(arr[i]);
   }
}

Select 1 option(s):
48.

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

public class OrderTest {

    public void initData(String[] arr){
        int ind = 0;
        for(String str : arr){
            str.concat(str+" "+ind);
            ind++;
        }
    }
    
    public void printData(String[] arr){
        for(var str : arr){
            System.out.println(str);
        }
    }
    
    public static void main(String[] args) {
        var ot = new OrderTest();
        String[] arr = new String[2];
        ot.initData(arr);
        ot.printData(arr);
    }
}

Select 1 option(s):
49.

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

class Names{
    private List list;
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public void printNames(){
        System.out.println(getList());
    }

    public static void main(String[] args) {
       List list = Arrays.asList(
        "Bob Hope",
        "Bob Dole",
        "Bob Brown"
       );

       Names n = new Names();
       n.setList(list.stream().collect(Collectors.toList()));
       n.getList().forEach(Names::printNames);
   }    

}

Select 1 option(s):
50.

Question:

Given:

   public static boolean getBool(){
      return true;
   }
   public static String getString(){
      return "true";
   }   
   public static void main(String args[]){
       switch( getBool() ){
            case true :
              System.out.println("true");
              break;
          default :
              System.out.println("none");
              break;
       }
   }

What changes can be done so that it will print only true?

Select 1 option(s):
51.

Question: What will the following program print when run?

public class ChangeTest {
private int myValue = 0;
public void showOne(int myValue){
myValue = myValue;
System.out.println(this.myValue);
}
public void showTwo(int myValue){
this.myValue = myValue;
System.out.println(this.myValue);
}
public static void main(String[] args) {
var ct = new ChangeTest();
ct.showOne(100);
ct.showTwo(200);
}
}

Select 1 option(s):

52.

Question:

Consider the following classes...

class Car{
public int gearRatio = 8;
public String accelerate() { return "Accelerate : Car"; }
}
class SportsCar extends Car{
public int gearRatio = 9;
public String accelerate() { return "Accelerate : SportsCar"; }
public static void main(String[] args){
Car c = new SportsCar();
System.out.println( c.gearRatio+" "+c.accelerate() );
}
}

What will be printed when SportsCar is run?

Select 1 option(s):
53.

Question:

Consider the following lines of code:

   System.out.println(null + true); //1
   System.out.println(true + null); //2
   System.out.println(null + null); //3


Which of the following statements are correct?

Select 1 option(s):
54.

Question:

Identify correct statements about -classpath and --module-path options while executing a java program.

Select 3 option(s):
55.

Question: 

Given:

interface Instrument{
  default void reset(){
     System.out.println("In Instrument");
  }
}

class Debt implements Instrument{
}

class Bond extends Debt{
  public void reset(){
     super.reset();
     System.out.println("In Bond");
  }
}

class ZCBond extends Bond{
  ZCBond(){
    reset();
  }
  public void reset(){
     super.reset();
     System.out.println("In ZCBond");
  }

  public static void main(String args[]){
        Instrument i = new ZCBond();
   }

}

What is the output?

Select 1 option(s):