wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Simulacro Java 21 - 5

Total questions: 55

Worksheet time: 2hrs 50mins

Name
Class
Date
1.

Which of the following is/are valid record definition(s)?. Please select 3 options.

a)
b)
c)
d)
e)
2.

Which of these expressions will obtain the substring "456" from a string defined by

String str = """

0123\

4567"""?

Please select 1 option

a)

str.substring(4, 7)

b)

str.substring(4)

c)

str.substring(5, 7)

d)

str.substring(5, 8)

e)

str.substring(4, 3)

3.

Java's exception mechanism helps in which of the following ways? Please select 2 options.

a)

It allows creation of new exceptions that are custom to a particular application domain.

b)

It improves code because error handling code is clearly separated from the main program logic.

c)

It enhances the security of the application by reporting errors in the logs.

d)

It improves the code because the exception is handled right at the place where it occured.

e)

It provides a vast set of standard exceptions that covers all possible exceptions.

4.

Please select 2 options.

a)

printUsefulData(al, (Data d)-> { return d.value>2; }  );

b)

printUsefulData(al, d-> d.value>2 );

c)

printUsefulData(al, (d)-> return d.value>2;  );

d)

printUsefulData(al, Data d-> d.value>2  );

e)

printUsefulData(al, d -> d.value>2;   );

5.

Please select 1 option.

a)

Replace the line at //1 with:

filterData(al, x -> x.value==0);  

b)

Add implements java.util.function.Predicate to MyFilter definition and replace the line at //1 with:

filterData(al, x -> x.value==0);  

c)

Add implements java.util.function.Predicate<Data> to MyFilter definition and replace the line at //1 with:

filterData(al, x -> x.value==0); 

d)

Remove MyFilter class altogether.Change type of f from MyFilter to java.util.function.Predicate in filterData method and replace the line at //1 with:

filterData(al, x -> x.value==0);  

e)

Remove MyFilter class altogether.

Change type of f from MyFilter to java.util.function.Predicate<Data> in filterData method and replace the line at //1 with:

filterData(al, x -> x.value==0);  

6.

Please select 1 option.

a)

Compiler error at line 3.

b)

Runtime error at line 3.

c)

It will print:

Automobile: drive

Truck: drive

Automobile: drive

in that order.

d)

It will print:

Automobile: drive

Truck: drive

Truck: drive

in that order.

e)

It will print:

Automobile: drive

Automobile: drive

Automobile: drive

in that order.

7.

Please select 1 option.

a)

Device should extend AutoCloseable and override close() method.

b)

Device should implement Closeable and override close() method.

c)

Device should implement Closeable and override autoClose() method.

d)

Device should implement AutoCloseable and override autoClose() method.

e)

Device should implement AutoCloseable and override close() method.

8.

Please select 1 option.

a)

mon

b)

thu

c)

fri

d)

It will not compile.

e)

It will throw an exception at run time.

9.

Please select 1 option.

a)

charlie andy

b)

andy charlie

c)

charlie charlie

d)

andy andy

e)

It will throw an exception at run time.

10.
a)

The program will fail to compile.

b)

Class cast exception at runtime.

c)

It will print 30, 20

d)

It will print 30, 30.

e)

It will print 20, 20.

11.

Please select 1 option.

a)

34

b)

38

c)

29

d)

25

12.

Please select 1 option.

a)

try{ 

     Files.move(Paths.get(s), Paths.get(d), StandardCopyOption.CREATE_NEW);      

Files.delete(Paths.get(s));

b)

try{      

Files.move(Paths.get(s), Paths.get(d), StandardCopyOption.REPLACE_EXISTING);

c)

try(FileChannel in = new FileInputStream(s).getChannel();    

 FileChannel out = new FileOutputStream(d).getChannel()){       

in.transferTo(0, in.size(), out);

}catch(Exception e){

}

d)

try{      

Files.move(Paths.get(s), Paths.get(d));

13.

Please select 1 option.

a)

appmessages_es_ES.properties is not present in PATH.

b)

appmessages_es_ES.properties is not present in CLASSPATH.

c)

appmessages_es_ES.properties is not specified using -D option on command line.

d)

appmessages_es_ES.properties is not present in JAVA_HOME.

14.

Given that your code is being run in a locale where the language code is fr and country code is CA, which of the following file names represents a valid resource bundle file name?. Please select 1 option.

a)

MyResource_fr_CA.xml

b)

MyResource_fr_CA.properties

c)

MyResource_fr_CA.java

d)

MyResource_fr_CA.profile

15.

Please select 2 options.

a)

If run with no arguments, the program will only print 'The end'.

b)

If run with one argument, the program will only print 'The end'.

c)

If run with one argument, the program will print 'Exception in Main' and 'The end'.

d)

If run with one argument, the program will only print 'Exception in Main'.

e)

If run with no arguments, the program will not print anything.

16.

Please select 1 option.

a)

wordStream.collect(Collectors.groupingBy(Collectors.counting()));

b)

wordStream.collect(Collectors.groupingBy(String::equals, Collectors.counting()));

c)

wordStream.collect(Collectors.groupingBy(String::toUpperCase, Collectors.counting()));

d)

wordStream.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

17.

Please select 1 option.

a)

Exception

false

b)

Exception

true

c)

Exception

<stack trace for a NullPointerException>

d)

Infinity

true

e)

Infinity

Exception

<stack trace for a NullPointerException>

18.

Please select 1 option.

a)

P-1D

PT-14H-50M

b)

P1D

PT9H10M

c)

P-1D

P-9H-10M

d)

P1D

PT14H50M

e)

It will throw an exception at run time.

19.

Given that a method named Double getPrice(String id) exists and may potentially return null, about which of the following options can you be certain that a runtime exception will not be thrown?. Please select 2 options.

a)

Optional<Double> price = Optional.of(getPrice("1111"));

b)

Optional<Double> price = Optional.ofNullable(getPrice("1111"));

Double x = price.orElse(getPrice("2222"));

c)

Optional<Double> price = Optional.ofNullable(getPrice("1111"));

Double y = price.orElseGet(()->getPrice("333"));

d)

Optional<Double> price = Optional.of(getPrice("1111"), 10.0);

e)

Optional<Double> price = Optional.of(getPrice("1111"));

Double z = price.orElseThrow(()->new RuntimeException("Bad Code"));

20.

Which of the following code fragments correctly executes the blog(String data) method of a blogging service that implements api.Blogger interface assuming that you already have a reference to the loader for this service? Assume that data refers to the data that needs to be blogged. Please select 2 options.

a)
b)
c)
d)
e)

for (Blogger b : loader){

    b.blog("Hello from textbook");

}

21.

Please select 1 option.

a)

1 ,2 ,3 4.

b)

1 ,4, 2 ,3

c)

3, 1, 2, 4

d)

2, 1, 4, 3

e)

2, 3, 1, 4

22.

Which of the following statements will print /test.txt when executed on a Unix/Linux system? Please select 1 option.

a)

System.out.println(Path.get("/test.txt"));

b)

System.out.println(new Path("/test.txt"));

c)

System.out.println(Paths.get("/", "test.txt"));

d)

System.out.println(Path.toPath("/test.txt"));

e)

System.out.println(Paths.toPath("/test.txt"));

23.

Please select 1 option.

a)

10000

Honda,Toyota

b)

10000.0

Honda,Toyota

c)

10000.0

Ford,Honda,Chevy,Toyota

d)

ClassCastException at run time.

e)

Compilation failure.

24.

Please select 1 option.

a)

for(int i=INT1; i<INT2; System.out.println(++i));

b)

for(int i=INT1; i++<INT2; System.out.println(i));

c)

int i=INT1; while(i++<INT2) { System.out.println(i); }

d)

int i=INT1; do { System.out.println(i); }while(i++<INT2);

e)

None of these.

25.

Identify correct statement(s) regarding the benefit of using PreparedStatement over Statement. Please select 1 option.

a)

PreparedStatement offers protection against SQL injection attacks.

b)

PreparedStatement allows transactions to span across multiple databases.

c)

PreparedStatement allows easier customization of joins at run time.

d)

PreparedStatement allows BLOB and CLOB on all databases.

26.

Please select 1 option.

a)

The FileWriter object will always be closed before the FileReader object.

b)

The order of the closure of the FileWriter and FileReader objects is platform dependent and should not be relied upon.

c)

The FileWriter object will not be closed if an exception is thrown while closing the FileReader object.

d)

This is not a fail safe approach to managing resources because in certain situations one or both of the resources may be left open after the end of the try block.

27.

Which of the following lambda expressions can be used to invoke a method that accepts a java.util.function.Predicate as an argument?. Please select 2 options.

a)

x -> System.out.println(x)

b)

x -> System.out.println(x);

c)

x -> x == null

d)

() -> true

e)

x->true

28.
a)

The main declares that it throws SomeThrowable but throws MyThrowable.

b)

You cannot have more than 2 classes in one file.

c)

The catch block in the main method must declare that it catches MyThrowable rather than SomeThrowable.

d)

There is nothing wrong with the code and Done will be printed.

29.

Please select 2 options.

a)

If IOException gets thrown at line1, then the whole method will end up throwing SQLException.

b)

If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException.

c)

If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException.

d)

If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException.

e)

If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException.

30.

Please select 1 option.

a)

stmt.commit();

b)

con.commit();

c)

stmt.commit(true);

d)

con.commit(true);

e)

No code is necessary.

31.

Please select 1 option.

a)

$20,000.00

20000

b)

$20,000.00

20K

c)

$20,000.00

200000.00

d)

20,000.00

20000

32.

Which of the following are benefits of ArrayList over an array?. Please select 1 option.

a)

You do not have to worry about the size of the ArrayList while appending elements.

b)

It consumes less memory space.

c)

You do not have to worry about thread safety.

d)

It allows you to write type safe code.

33.

Please select 2 options

a)

Object o = a; Runnable r = o;

b)

Object o = a; Runnable r = (Runnable) o;

c)

Object o = a; Observer ob = (Observer) o ;

d)

Object o = b; Observer o2 = o;

e)

Object o = b; Runnable r = (Runnable) b;

34.

Given:

var sList = new CopyOnWriteArrayList<Student>();

which of the following statements are correct?. Please select 1 option.

a)

Student class must be Cloneable or Serializable

b)

Student objects retrieved from sList are thread safe.

c)

Multiple threads can get Student objects from sList but can't add to it in a thread safe manner simultaneously.

d)

Multiple threads can safely add and remove objects from sList simultaneously.

e)

Any code that adds objects to sList must be synchronized but code that gets objects need not be.

35.

Please select 1 option.

a)

-9

b)

-10

c)

-11

d)

-12

36.

Yoy want to use a third party JDBC driver for a database. Which of the following actions must you take to retrieve a Connection using that driver in your JDBC program? Please select 2 options.

a)

Put the driver jar in the class path.

b)

Load the driver class using Class.forName

c)

Create an instance of the Driver class using the new keyword.

d)

Retrieve a connection using DriverManager.getConnection.

e)

Load the driver class using Class.forName and then retrieve a connection using DriverManager.getConnection.

37.

Please select 3 options

a)

return new ArrayList<Integer>();

b)

return new ArrayList<Number>();

c)

return new ArrayList<Object>();

d)

return new ArrayList();

e)

return new ArrayList<? super Number>();

38.

Please select 1 option

a)

IllegalArgumentException

b)

ArrayIndexOutOfBoundsException

c)

c:\

d)

c:

e)

main

39.

Please select 1 option

a)

//1, //2, and //3 correctly override the method in Base.

b)

//2, //3, and //4 correctly overload the method in Base

c)

//2 and //3 correctly override the method in Base.

d)

//1 correctly overloads while //4 correctly overrides the method in Base.

e)

//1 correctly overrides the method in Base.

40.

Which of these statements are true? Please select 2 options.

a)

If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.

b)

An overriding method must declare that it throws the same exception classes as the method it overrides.

c)

The main method of a program can declare that it throws checked exceptions.

d)

A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.

e)

finally blocks are executed if and only if an exception gets thrown while inside the corresponding try block.

41.

Please select 1 option.

a)

Mysql driver jar and datalayer.jar must first be converted into modular jars.

b)

datalayer.jar must first be converted into modular jar. The mysql jar need not be converted.

c)

The mysql jar must first be converted into modular jar. The datalayer.jar need not be converted.

d)

Neither datalayer nor mysql driver need to be converted into modular jars.

42.

Which of the following statements are correct about --jdk-internals option of jdeps tool when run on a jar file or a class file? . Please select 2 options.

a)

It analyzes all classes of the given jar file for class level dependence on Java API.

b)

It analyzes all classes of the given jar file for class level dependence on jdk's deprecated API. If any such dependence is found, it is printed with a suggestion for replacement.

c)

It analyzes all classes of the given jar file for class level dependence on jdk's internal API. If any such dependence is found, it is printed with a suggestion for replacement.

d)

It analyzes all input modules and list all jdk internal modules on which the input modules depend. If any such dependence is found, it is printed with a suggestion for replacement.

e)

It performs static analysis.

43.
a)

123

b)

246

c)

Compilation failure

d)

exception at run time

44.

Please select 1 option.

a)

It will not compile.

b)

It will compile but throw an exception at runtime.

c)

It will print 'null' (without quotes).

d)

It will print nothing.

e)

None of the above.

45.

Please select 1 option.

a)

13.0

b)

4.0

c)

4

d)

The code will not compile.

46.

Please select 1 option.

a)

A compilation error will occur.

b)

10.0

12.0

c)

20.0

22.0

d)

It will compile but throw an exception at run time.

47.

Please select 3 options.

a)

jdeps -cp file1.jar;file2.jar app.jar

b)

jdeps --upgrade-module-path file1.jar;file2.jar app.jar

c)

jdeps --class-path file1.jar;file2.jar app.jar

d)

jdeps -classpath file1.jar;file2.jar app.jar

e)

jdeps app.jar

48.

Please select 1 option.

a)

5 5

b)

5 6

c)

6 6

d)

6 5

e)

4 5

49.

Identify correct statements about the modular JDK. Please select 3 options.

a)

The foundational APIs of the Java SE platform are found in java.base module.

b)

The modular JDK is composed of two modules - the java module and the jdk module.

c)

JDK is divided into a set of modules that can be combined at compile time, build time, and run time into a variety of configurations.

d)

The modular JDK is divided of two kinds of modules - the standard modules and the non-standard modules.

50.

Please select 1 option.

a)

Compilation error at //1.

b)

Compilation error at //2.

c)

Compilation error at //3.

d)

USA, UK, Netherlands, India, France

e)

USA, Netherlands, UK, India, France

51.

Please select 1 option.

a)

It will not compile.

b)

It will print 10 and then 10

c)

It will print 20 and then 20

d)

It will print 10 and then 20

e)

It will print 20 and then 10

52.

Please select 1 option.

a)

ls.stream().map(a->a).max();

b)

ls.stream().max(Comparator.comparing(a->a)).get();

c)

ls.stream().reduce((a, b)->a>b?a:b);

d)

All of above.

e)

None of the above.

53.

Please select 1 option.

a)

Compilation error.

b)

Exception at run time.

c)

2015-12-31

d)

2015-11-30

54.

Please select 3 options.

a)

package p1;

public class CompoundIntCalculator extends InterestCalculator{ }

b)

package p1;

non-sealed abstract class CompoundIntCalculator extends InterestCalculator{ }

c)

package p1.p2;

public non-sealed class CompoundIntCalculator extends InterestCalculator{ }

d)

package p1;

final class CompoundIntCalculator extends InterestCalculator{ }

e)

package p1;

public non-sealed class CompoundIntCalculator extends InterestCalculator{ }

55.

Please select 1 option.

a)

It will print MR, MS1, MS2, but the order is unpredictable.

b)

MS2, MR, MS1,

c)

MR, MS1, MS2,

d)

It will not compile.