Welcome to your Test - 7 Code - 819

Name
Email
Phone
1.

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

class A{
   final int fi = 10;
}
public class B extends A{
   int fi = 15;
   public static void main(String[] args){
       B b = new B();
       b.fi = 20;
       System.out.println(b.fi);
       System.out.println(  (  (A) b  ).fi  );
   }
}

Select 1 option(s):
2.

Question:

Your finance library contains the following two public classes:

public final class BalanceSheeData implements Cloneable{
    private String xmlDoc;
    public String getDoc(){
        return xmlDoc;
    }
    
    //other methods not shown    
}

public class ProcessFinancialStatements{
   private boolean validate(BalanceSheeData bsd){
      //check validity and return true false accordingly
   }
   public void process(BalanceSheeData bsd){
       if(validate(bsd)) {
          doSomething(bsd.clone());
       }
   }
}


Which safety guideline does the above code violate?

Select 1 option(s):
3.

Question:

Given:

var al = new ArrayList<String>();
    al.forEach( k -> {
             System.out.print(k.length());    
         });


Identify correct statements.

Select 1 option(s):
4.

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

// Filename: TestClass.java
public class TestClass{
public static void main(String args[]){
B c = new C();
System.out.println(c.max(10, 20));
}
}
class A{
int max(int x, int y) { if (x>y) return x; else return y; }
}
class B extends A{
int max(int x, int y) { return 2 * super.max(x, y) ; }
}
class C extends B{
int max(int x, int y) { return super.max( 2*x, 2*y); }
}

Select 1 option(s):

5.

Question:

Identify correct constructs.

Select 1 option(s):
6.

Question:

Consider the following code snippet ...

boolean[] b1 = new boolean[2];
boolean[] b2 = {true , false};
System.out.println( "" + (b1[0] == b2[0]) + ", "+ (b1[1] == b2[1])  );


What will it print ?

Select 1 option(s):
7.

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

public class ATest {
    
    String global = "111";
    
    public int parse(String arg){
        var value = 0;
        try{
            String global = arg;            
            value = Integer.parseInt(global);
        }
        catch(Exception e){
            System.out.println(e.getClass());
        }
        System.out.print(global+" "+value+" ");
        return value;
    }
       public static void main(String[] args) {
          ATest ct = new ATest();
          System.out.print(ct.parse("333"));
       }
}

Select 1 option(s):
8.

Question:

Consider the following code:

class LowBalanceException extends _______ {  //1
    public LowBalanceException(String msg){ super(msg);    }
}

class WithdrawalException extends _______ { //2
    public WithdrawalException(String msg){ super(msg);    }
}
class Account{
    double balance;
    public void withdraw(double amount) throws WithdrawalException{
        try{
            throw new RuntimeException("Not Implemented");
        }catch(Exception e){
            throw new LowBalanceException(e.getMessage());
        }
    }
    public static void main(String[] args) {
        try{
            Account a = new Account();
            a.withdraw(100.0);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}


What can be inserted at //1 and //2 so that the above code will prints Not Implemented?.

Select 1 option(s):
9.

Question: What is the result of compiling and running this code?

class MyException extends Throwable{}
class MyException1 extends MyException{}
class MyException2 extends MyException{}
class MyException3 extends MyException2{}
public class ExceptionTest{
   void myMethod() throws MyException{
      throw new MyException3();
   }
   public static void main(String[] args){
      ExceptionTest et = new ExceptionTest();
      try{
         et.myMethod();
      }
      catch(MyException me){
         System.out.println("MyException thrown");
      }
      catch(MyException3 me3){
         System.out.println("MyException3 thrown");
      }
      finally{
         System.out.println(" Done");
      }
   }
}

Select 1 option(s):
10.

Question:

Given:

class Triangle{
public int base;
public int height;
private final double ANGLE;
public void setAngle(double a){ ANGLE = a; }
public static void main(String[] args) {
var t = new Triangle();
t.setAngle(90);
}
}

Identify the correct statement(s).

Select 1 option(s):
11.

Question: What will the following program print?

class Test{
public static void main(String args[]){
var c = 0;
var flag = true;
for(var i = 0; i < 3; i++){
while(flag){
c++;
if(i>c || c>5) flag = false;
}
}
System.out.println(c);
}
}

Select 1 option(s):

12.

Question: What will the following code snippet print?

int index = 1;
String[] strArr = new String[5];
var myStr  = strArr[index];
System.out.println(myStr);

Select 1 option(s):
13.

Question: Which of these are valid expressions to create a string of value "hello world" ?

Select 3 option(s):
14.

Question: What letters, and in what order, will be printed when the following program is compiled and run?

public class FinallyTest{
   public static void main(String args[]) throws Exception{
       try{
          m1();
          System.out.println("A");
       }
       finally{
          System.out.println("B");
       }
       System.out.println("C");
   }
   public static void m1() throws Exception { throw new Exception(); }
}

Select 1 option(s):
15.

Question:

Given:

public static void main(String[] args) {
    INSERT CODE HERE
    List.of(args).forEach(x);
}

Which of the following options can be inserted in the above code without causing a compilation error?

Select 2 option(s):
16.

Question:

Given:


int expr1 = 3 + 5 * 9 - 7;        
int expr2 = 3 + (5 * 9) - 7;        
int expr3 = 3 + 5 * (9 - 7);        
int expr4 = (3 + 5) * 9 - 7;                


Which of the above variables will have the value 45?

Select 1 option(s):
17.

Question: What will be the result of trying to compile and execute the following program?

public class TestClass{
   public static void main(String args[] ){
      var i = 0 ;
      int[] iA = {10, 20} ;
      iA[i] = i = 30 ;
      System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + "  "+i) ;
    }
}

Select 1 option(s):
18.

Question: What will the following code print when run?

public class TestClass {
    public static void m1() throws Exception{
        throw new Exception("Exception from m1");
    }
    public static void m2() throws Exception{
        try{
            m1();
        }catch(Exception e){
            //Can't do much about this exception so rethrow it
            throw e;
        }finally{
            throw new RuntimeException("Exception from finally");
        }
        
    }
    
    public static void main(String[] args) {
        try{
            m2();
        }catch(Exception e){
            Throwable[] ta = e.getSuppressed();
            for(Throwable t : ta) {
                System.out.println(t.getMessage());
            }
        }
    }   
}

Select 1 option(s):
19.

Question: What, if anything, is wrong with the following code?

//Filename: TestClass.java
class TestClass implements T1, T2{
   public void m1(){}
}
interface T1{
   int VALUE = 1;
   void m1();
}
interface T2{
   int VALUE = 2;
   void m1();
}


Select 1 option(s):
20.

Question: What will the following code print?

        int x = 1;
        int y = 2;
        int z = x++;
        int a = --y;
        int b = z--;
        b += ++z;

        int answ = x>a?y>b?y:b:x>z?x:z;
        System.out.println(answ);

Select 1 option(s):
21.

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

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
   public static void main(String [] args) throws Exception{
      try{
         m2();
      }
      finally{ m3(); }
    }
    public static void m2() throws NewException{  throw new NewException();  }
    public static void m3() throws AnotherException{  throw new AnotherException();  }
}

Select 1 option(s):
22.

Question:

Given:

class MyGenericClass<T>{
    public <T> String transform(T t){
        return t.toString()+"-"+t.hashCode();
    }
}

and the following code snippet:

MyGenericClass gc = new MyGenericClass();
System.out.println(gc.transform(1)); //1
System.out.println(gc.transform("hello")); //2
MyGenericClass<String> gcStr = new MyGenericClass<String>();
System.out.println(gcStr.transform(1.1)); //3


What will it print when compiled and run?

Select 1 option(s):
23.

Question:

Identify correct option(s):

Select 2 option(s):
24.

Question:

Given:

public static void generateMultiplicationTable(int number){
    Stream<Integer> sin = Stream.of(1, 2, 3 );
    Consumer<Integer> c1 = System.out::print;
    Consumer<Integer> c2 = x->{ System.out.println(" * "+number+" = "+x*number); };

    INSERT CODE HERE
}
public static void main(String[] args) throws Exception{
    generateMultiplicationTable(2);
}


Which of the options, when inserted in the above code, will produce the following output:

1 * 2 = 2
2 * 2 = 4
3 * 2 = 6

Select 1 option(s):
25.

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

ArrayList<String> als = new ArrayList<>(List.of("a", "b", "c"));
Set<String> ss = new HashSet();
ss.addAll(als); //1
als.clear();    //2
System.out.println(ss.size());

Select 1 option(s):
26.

Question:

Given:

var nums = List.of(1, 2, 3, 4).stream();
Which of the following options will compute the average of the numbers in the nums stream?

Select 2 option(s):
27.

Question:

You are using a RDBMS database from a company named Fandu Tech. It provides a JDBC 4.0 compliant driver implementation in class com.fandu.sql.Driver.

Which of the following lines of code is/are required to get the driver loaded?

Select 1 option(s):
28.

Question:

Consider the following code fragment:

    public static void myMethod(int x)  //Specify throws clause here
    {
        try{
            if(x == 0){
                throw new ClassNotFoundException();
            }
            else throw new NoSuchFieldException();
        }catch(RuntimeException e){
            throw e;
        }
    }


Which of the following is a valid throws clause for the above method?

Select 2 option(s):
29.

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

public class TestClass{
   public static void main(String[] args){
        int[] arr = { 1, 2, 3, 4, 5, 6 };
        int counter = 0;
        for (var value : arr) {
           if (counter >= 5) {
               break;    
           } else {
            continue;    
           }
           if (value > 4) {
             arr[counter] = value + 1;    
           }    
           counter++;
        }
        System.out.println(arr[counter]);
   }

}

Select 1 option(s):
30.

Question:

Identify correct statements about java modules.

Select 2 option(s):
31.

Question:

Consider the following classes :

class A{
public void mA(){ };
}
class B extends A {
public void mA(){ }
public void mB() { }
}
class C extends B {
public void mC(){ }
}

and the following declarations:

A x = new B(); B y = new B(); B z = new C();

Which of the following calls are virtual calls?

Select 3 option(s):
32.

Question:

An existing application contains the following code:

interface AmountValidator{
    public boolean checkAmount(double value);
}
public class Account {
    public void updateBalance(double bal ){

        boolean isOK = new AmountValidator(){
            public boolean checkAmount(double val){
                return val >= 0.0;
            }
        }.checkAmount(bal);
      
        //other irrelevant code
    }
}

Which interface from java.util.function package should be used to refactor this code?

Select 1 option(s):
33.

Question:

Identify correct statements about the following code:

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

    }
}


Select 1 option(s):
34.

Question:

You have an existing java application that contains the following packages -

com.acme.provisioning.client
com.acme.provisioning.service
com.acme.provisioning.dto
com.acme.provisioning.database

The dependency of classes in these packages is as follows -

Classes in the client package use classes from the service and the dto package.
Classes in the service package use classes from the dto and the database package
Classes in the database package use classes from the dto package.
Classes in the dto package do not use classes from any other package.

You want to modularize the application into three modules - client, service, and dto.
Which of the following would be a valid module-info for the service module?

Select 1 option(s):
35.

Question: Which functional interface(s) would you use if you need a function that always returns the same type as the type of its input(s)?

Select 2 option(s):
36.

Question: What will the following code print?

    String s = "blooper";
    StringBuilder sb = new StringBuilder(s);
    sb.append(s.substring(4)).delete(3, 5);
    System.out.println(sb);

Select 1 option(s):
37.

Question:

Consider the following code:

class OuterWorld
{
    public InnerPeace i = new InnerPeace("none"); //1
    class InnerPeace
    {  
        private String reason = "none";
        InnerPeace(String reason){ this.reason = reason; }
    }
    public static void main(String[] args){
        var ip = new InnerPeace("yoga"); //2
        var out = new OuterWorld();
        System.out.println(out.i.reason); //3
    }
}

Identify correct statements about the above code.

Select 2 option(s):
38.

Question:

Assuming that Book has appropriate constructor and accessor methods, what will the following code print?

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

books.stream()
          .filter(b->b.getTitle().startsWith("F"))
          .forEach(b->b.setPrice(10.0));
books.stream()
          .forEach(b->System.out.println(b.getTitle()+":"+b.getPrice()));


Select 1 option(s):
39.

Question: Which of the following statements are true regarding the try-with-resources statement?

Select 1 option(s):
40.

Question:

Given:

var books = new ArrayList<Book>(List.of(new Book("The Outsider", "Stephen King"),
        new Book("Becoming", "Michelle Obama" ), new Book("Uri", "India")));
Stream bkStrm = books.stream(); //1
int count = bkStrm.peek(x->x.getTitle()).count();//2


Assuming that the Book class has appropriate constructor and method, what changes can be made to the above code independent of each other to make it will compile ?

Select 1 option(s):
41.

Question:

What will the following code print when run?

public class TestClass {
    public static void main(String[] args) throws Exception { //1

         var flag  = true; //2
         switch (flag){ //3
             case true : System.out.println("true");
                 default: System.out.println("false");
         }
              
    }
}


Select 1 option(s):
42.

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

public class TestClass{
public void testRefs(String str, StringBuilder sb){
str = str + sb.toString();
sb.append(str);
str = null; sb = null;
}
public static void main(String[] args){
String s = "aaa"; StringBuilder sb = new StringBuilder("bbb");
new TestClass().testRefs(s, sb);
System.out.println("s="+s+" sb="+sb);
}
}

Select 1 option(s):

43.

Question: Which of these array declarations and initializations are NOT legal?

Select 2 option(s):
44.

Question:

You are developing an Address class. Among others, it has a field for "country". You allow only a few country codes to be used for this field. You want to keep this list of valid country codes in your class. This list can be accessed by other classes if they want to know which valid country values do you allow.

Which of the following options will you use?

Select 1 option(s):
45.

Question:

Given:

//in file Movable.java
package p1;
public interface Movable {
  int location = 0;
  void move(int by);
  public void moveBack(int by);
}


//in file Donkey.java
package p2;
import p1.Movable;
public class Donkey implements Movable{
    int location = 200;
    public void move(int by) {
        location = location+by;
    }
    public void moveBack(int by) {
        location = location-by;
    }
}


//in file TestClass.java
package px;
import p1.Movable;
import p2.Donkey;
public class TestClass {
    public static void main(String[] args) {
        Movable m = new Donkey();
        m.move(10);
        m.moveBack(20);
        System.out.println(m.location);
    }
}

Identify the correct statement(s).

Select 1 option(s):
46.

Question:

Encapsulation ensures that ...

Select 1 option(s):
47.

Question: What will the following code snippet print?

  Object t = new Integer(107);
   int k = (Integer) t.intValue()/9;
   System.out.println(k);


Select 1 option(s):
48.

Question:

A java programer has written the following code:

public class Book {
private int id;
private String title;
private String genre;
private String author;

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

    //accessors for instance fields not shown here
    
    public static class BookFilter {
        public boolean isFiction(Book b){
            return b.getGenre().equals("fiction");
        }

    }
}


He wants to make use of the above code in another class as follows -

List<Book> books = Arrays.asList(new Book("30 Days", "fiction", "K Larsen"),
      new Book("Fast Food Nation", "non-fiction", "Eric Schlosser"),
      new Book("Wired", "fiction", "D Richards"));
        
books.stream()

      .filter(new Book.BookFilter()) //LINE 10

      .forEach((Book b) -> System.out.print(b.getTitle()+", "));


What changes mentioned below can he make independent of each other so that the above code will print 30 Days, Wired, ?

Select 2 option(s):
49.

Question:

Identify correct statements about the following code:

class Account {
    int id;
    double balance;
    @Deprecated
    public Account(){
    };
    public Account(int id){       this.id = id;    };   
    public void transact(double amount){ balance = balance+amount; }
}

class NewAccount extends Account{
    public NewAccount(){ }
}
public class TestClass {
    public static void main(String[] args) {
        Account c = new NewAccount();
    }
}


Select 1 option(s):
50.

Question:

Given:

public class LoopTest {
    int k = 5;
    public boolean checkIt(int k){
        return k-->0?true:false;
    }
    public void printThem(){
        while(checkIt(k)){
            System.out.print(k);
        }
    }
    public static void main(String[] args) {
        new LoopTest().printThem();
    }
}

What changes should be made so that the program will print 54321?

Select 1 option(s):