Java Programming Practice MCQs Codetantra. CBT-5
- Lambda Expressions
- Date Time (Utility Classes)
- Exception Handling
01. Choose the correct option for the code given below:
class Main {
String dummy = "5";
void property1() {
try {
dummy = dummy + "11";
property2();
}
catch (Exception exc) {
dummy = dummy + "29";
}
}
void property2() throws Exception {
try {
dummy = dummy + "39";
property3();
}
catch(Exception e) {
throw new Exception();
}
finally {
dummy = dummy + "55";
}
dummy = dummy + "173";
}
void property3() throws Exception {
throw new Exception();
}
void showTime() {
System.out.println(dummy);
}
public static void main(String[] args) {
Main mainObject = new Main();
mainObject.property1();
mainObject.showTime();
}
}
02. Choose the correct option for the code given below:
class Main {
int dummy = 65;
void compute() throws Exception {
try {
dummy += 3;
try {
dummy += 3;
try {
dummy += 3;
throw new Exception();
}
catch(Exception exc) {
dummy += 3;
throw new Exception();
}
}
catch(Exception ex) {
dummy += 3;
}
}
catch(Exception ex) {
dummy += 3;
}
}
void showTime() {
System.out.println(dummy);
}
public static void main(String[] args) throws Exception {
Main object = new Main();
object.compute();
object.showTime();
}
}
03. Find out the correct results of given code snippet:
import java.util.*;
class Main {
public static void main(String args[]) {
int array1[] = { 3, 52, 10, 25, 42, 18, 35 };
int array2[] = Arrays.copyOfRange(array1, 2, 6);
for (int i : array2)
System.out.print(i + " ");
System.out.println();
int[] array3 = Arrays.copyOfRange(array1, 8, array1.length + 3);
for (int i : array3)
System.out.print(i + " ");
}
}
04. Predict the right option for the below given code snippet.
public class Main {
static int x, y;
public static void main(String args[]) {
try{
A();
x = 10; y = 3;
}
catch(RuntimeException ex) {
System.out.print("81");
}
catch(Exception ex) {
System.out.print("67");
}
System.out.print("54");
}
private static void A() {
try {
B();
}
finally {
System.out.print("8");
}
}
private static void B() {
try {
int res = x/y;
System.out.print(res);
}
catch(NumberFormatException | NullPointerException ex) {
System.out.print("39");
}
}
}
05. Predict the right option for the below given code snippet.
public class Main {
static int x, y;
public static void main(String args[]) {
try{
A();
x = 10; y = 3;
}
catch(RuntimeException ex) {
System.out.print("92");
}
catch(Exception ex) {
System.out.print("78");
}
System.out.print("45");
}
private static void A() {
try {
B();
}
finally {
System.out.print("20");
}
}
private static void B() {
try {
int res = x/y;
System.out.print(res);
}
catch(NumberFormatException | ArithmeticException ex) {
System.out.print("31");
}
}
}
06. Choose the correct option for the code given below:
class Main {
public static void main(String args[]) {
try {
int a = 82;
throw ++a;
}
catch(int status) {
System.out.println(status);
}
}
}
07. Choose the correct option for the code given below:
class Main {
public static void main(String args[]) {
try {
throw new Exceptions();
}
catch(Exceptions t) {
System.out.println("34");
}
finally {
System.out.println("199");
}
}
}
class Exceptions extends Exception {
}
08. Choose the correct option for the code given below:
class Main {
public static void main(String args[]) {
try {
throw new Exceptions();
}
catch(Exceptions t) {
System.out.println("76");
}
finally {
System.out.println("140");
}
}
}
class Exception extends Exceptions {
}
09. Choose the correct option for the code given below:
public class Main {
public static void main(String args[]) {
try {
int a = 34;
int b = a * --a;
if(b > (a * a))
throw new B();
}
catch(A object1) {
System.out.println("37");
}
catch(B object2) {
System.out.println("70");
}
finally{
System.out.println("102");
}
}
}
class A extends Exception {
}
class B extends A {
}
10. Choose the correct option for the code given below:
class Main {
public static void main(String[] args) {
try {
int array[]= {1, 18, 25, 32, 87};
for (int i = 0; i <= 5; i++) {
System.out.print(array[i] + " ");
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("37");
}
catch (Exception e) {
System.out.println (e);
}
}
}
11. Choose the correct option for the code given below:
class Main {
public static void main(String[] args) {
try {
int array[]= {9, 16, 23, 49, 78};
for (int i = 0; i <= 5; i++) {
System.out.print(array[i] + " ");
}
}
catch (Exception e) {
System.out.println (e);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("21");
}
}
}
12. Predict the correct output of below given code:
interface A {
void function1(int x, int y);
default void function2(int x, int y) {
System.out.println(x + y);
}
}
class Main {
public static void main(String args[]) {
A a = (int x, int y)->System.out.println(2*x + 2*y);
a.function1(27,59);
a.function2(41,74);
}
}
13. Predict the correct output of below given code:
interface A {
void function1(int x, int y);
default void function2(int x, int y) {
System.out.println(x + y);
}
}
class Main {
public static void main(String args[]) {
A a = ()->System.out.println("8");
a.function1();
a.function2(22,54);
}
}
14. Predict the correct output of below given code:
interface A {
void function1();
default void function2(int x, int y) {
System.out.println(x + y);
}
}
class Main {
public static void main(String args[]) {
A a = ()->System.out.println("11");
a.function1();
a.function2(26,58);
}
}
15. Predict the correct output of below given code:
interface Interface {
void property1(int x);
default void property2(int x, int y ) {
System.out.println(++x + y++);
}
}
class Main {
public static void main(String args[]) {
Interface a = (int p)->System.out.println(p++);
a.property1(11);
a.property2(93,125);
}
}
16. Predict the right option for the given code snippet:
interface B {
Number m(int x) {
return x;
}
}
public class Main {
public static void main(String args[]) {
B b = (int x) -> (9 * 2);
System.out.print(b.m());
}
}17. Predict the right option for the given code snippet:
interface Fruit {
Number apple(int x);
}
public class Main {
public static void main(String args[]) {
Fruit b = (int x) -> (13 * 5);
System.out.print(b.apple(18));
}
}
18. Predict the correct output of below given code snippet:
import java.util.*;
public class Main {
public static void main(String[] args) {
int array[] = {9, 15, 22, 13};
int keyElement = 24;
Arrays.fill(array, ++keyElement);
System.out.println(Arrays.toString(array));
}
}
19. Predict the correct output of below given code snippet:
import java.util.*;
public class Main {
public static void main(String[] args) {
int array[] = { 14, 78, 42, 7, 71 };
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(Arrays.copyOf(array, 8)));
}
}
20. Predict the correct output of below given code snippet:
import java.util.*;
public class Main {
public static void main(String[] args) {
int array[] = { 264, 241, 128, 60, 42 };
Arrays.sort(array, 1, 2);
System.out.println(Arrays.toString(array));
}
}
21. Predict the correct output of below given code snippet:
Assume current date: 1984-March-28
current time: 7:16:53 AM (HH:MM:SS)
import java.text.*;
import java.util.*;
class Main {
public static void main(String[] args) {
Date date = new Date();
System.out.println(DateFormat.getDateInstance().format(date));
System.out.println(DateFormat.getTimeInstance().format(date));
System.out.println(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date));
System.out.println(DateFormat.getTimeInstance(DateFormat.SHORT).format(date));
}
}
22. Predict the correct output of below given code snippet:
Assume current date: 2019-March-8
current time: 10:32:11 AM (HH:MM:SS)
import java.text.*;
import java.util.*;
class Main {
public static void main(String[] args) {
Date date = new Date();
System.out.println(DateFormat.getDateTimeInstance().format(date));
System.out.println(DateFormat.getTimeInstance(DateFormat.LONG).format(date));
}
}
23. Analyse the code and select the correct option:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array1 = new int [] {1, 2, 1};
int[] array2 = new int [] {1, 2, 1};
Boolean a = Arrays.equals(array1, array2);
if(a) {
System.out.print("102");
}
else {
System.out.print("184");
}
}
}
24. Predict the right option for the below given code snippet.
class X extends Exception{ }
class Y extends X { }
public class Main {
public static void main(String [] args) {
try {
demo();
}
catch (X obj) {
System.out.println (obj);
}
}
public static void demo() throws Y {
throw new Y();
}
}
25. Predict the right option for the below given code snippet.
class X extends Exception{ }
class Y extends X { }
public class Main {
public static void main(String [] args) {
try {
demo();
}
catch (X obj) {
System.out.println (obj);
}
}
public static void demo() throws Y {
throw new X();
}
}

.jpg)