Java Programming Practice MCQs Codetantra. CBT-3
01. Predict the correct output:
import java.io.*;
class Main {
public static void main(String[] args) {
Integer a = new Integer(418);
int b = a;
System.out.println(a);
System.out.println(b);
Character c = '4';
char d = c;
System.out.println(c);
System.out.println(d);
}
}
02. If code executes fine then predict the correct output otherwise choose the suitable option.
class Code {
int a;
}
public class Main {
public static void main(String[] args) {
Code c = new Code();
c.a = 24;
Code d = new Code();
d.a = 7;
Compute(c, d);
System.out.println(c.a + " " + d.a);
}
public static void Compute(Code c, Code d) {
c.a = c.a + d.a;
d.a = c.a * d.a;
int temp = c.a;
c.a = d.a;
d.a = temp;
}
}
03.
What are the contents array after the following code has been executed?
int[][] array = {{5,8,3},{2,7,5}};
int value = 0;
for (int row = 1; row < array.length; row++) {
for (int col = 1; col < array[0].length; col++) {
if (array[row][col] % 2 == 1) {
array[row][col] = array[row][col] + 1;
}
else if (array[row][col] % 2 == 0) {
array[row][col] = array[row][col] * 2;
}
}
}
04. If code executes fine then predict the correct output otherwise choose the suitable option.
class Code {
int a;
}
public class Main {
public static void main(String[] args) {
Code c = new Code();
c.a = 14;
Code d = new Code();
d.a = 45;
Compute(c, d);
System.out.println(c.a + " " + d.a);
}
public static void Compute(Code c, Code d) {
c.a = c.a + d.a;
d.a = c.a * d.a;
int temp = c.a;
c.a = d.a;
d.a = c.a;
c.a = temp;
}
}
05 What will be the correct output of below given code?
class MainSubClass {
void f(int i) {
System.out.print(i);
}
}
public class Main extends MainSubClass {
void f(int i) {
System.out.print(10*i);
}
public static void main(String[] args) {
MainSubClass x = new Main();
Main y = new Main();
Main z = new Main();
x.f(31);
((MainSubClass)y).f(63);
z.f(190);
}
}
07. If code executes fine then predict the correct output otherwise choose the suitable option.
class Code {
final public void showTime() {
System.out.println("885");
}
}
class Tantra extends Code {
public void showTime() {
System.out.println("-460");
}
}
public class Main {
public static void main(String[] args) {
Code b = new Tantra();
b.showTime();
}
}
08. Predict the correct results of below given code snippet:
abstract class CT {
public int x;
CT() {
x = 6;
}
abstract public void showTime();
abstract final public void funTime();
}
class Main extends CT {
public void showTime(int a) {
this.x = a;
}
final public void funTime() {
System.out.println(x);
}
public static void main(String[] args) {
Main object = new Main();
object.showTime(169);
object.funTime();
}
} 09. Predict the correct results of the code snippet given below:
class D {
static int x;
protected static void getMethod() {
System.out.println("9");
++x;
}
}
public class Main extends D {
protected static void getMethod() {
System.out.println("20");
++x;
}
public static void main(String[] args) {
D object = new D();
object.getMethod();
System.out.print(x);
}
}
10.What is the output of the following code
public class Main {
public static void main(String[] args) {
int[][] matrix =
{{2, 9, 13, 16},
{24, 28, 31, 39},
{5, 3, 2, 1},
{1, 9, 9, 8}};
for (int i = 0; i < 4; i++)
System.out.print(matrix[1][i] + " ");
}
}11. Decode the code and choose the correct results:
abstract class A {
final void showTime() {
System.out.println("4323");
}
}
class B extends A {
static void funTime() {
System.out.println("6896");
}
}
public class Main {
public static void main(String args[]) {
A object = new B();
object.showTime();
B.funTime();
}
}
12. What is the output of the following
class Atom {
Atom() {
System.out.print("7 ");
}
}
class Rock extends Atom {
Rock(String type) {
System.out.print(type);
}
}
public class Mountain extends Rock {
Mountain() {
super("14 ");
new Rock("14 ");
}
public static void main(String[] a) {
new Mountain();
}
}
13. Predict the correct results of below given code snippet:
import java.io.*;
interface a {
static int x = 36;
default void showTime2() {
System.out.println("68");
x++;
}
}
class Main implements a {
public void showTime1() {
System.out.println("19");
x++;
}
public static void main(String[] args) {
Main test = new Main();
test.showTime1();
System.out.println(x);
test.showTime2();
System.out.println(x);
}
}
14. Predict the correct results of below given code snippet:
import java.io.*;
interface a {
final int x = 32;
default void showTime2() {
System.out.println("65");
}
}
class Main implements a {
public void showTime1() {
System.out.println("15");
}
public static void main(String[] args) {
Main test = new Main();
test.showTime1();
System.out.println(x);
test.showTime2();
System.out.println(x);
}
}
15. What is the output of the following code
class A {
public int i;
protected int j;
}
class B extends A {
int j;
void display() {
super.j = 1;
System.out.println(i + " " + j);
}
}
class Output {
public static void main(String args[]) {
B obj = new B();
obj.i = 9;
obj.j = 15;
obj.display();
}
}
16. Predict the correct results of below given code snippet:
import java.io.*;
interface a {
final int x = 114;
void showTime();
}
class Main implements a {
public void showTime() {
System.out.println("31");
}
public static void main(String[] args) {
Main test = new Main();
test.showTime();
System.out.println(x);
}
}
17. Predict the correct results of below given code snippet:
abstract class B {
B() {
System.out.println("42");
}
static void funTime1() {
System.out.println("188");
}
}
class D extends B {
D() {
System.out.println("124");
}
static void funTime2() {
System.out.println("57");
}
}
class Main {
public static void main(String args[]) {
D object = new D();
D.funTime2();
B.funTime1();
}
}
18. If code executes fine then predict the correct output otherwise choose the suitable option.
class Code {
public void showTime() {
System.out.println("1016");
}
}
class Tantra extends Code {
public void showTime() {
System.out.println("-197");
}
}
public class Main {
public static void main(String[] args) {
Code b = new Tantra();
b.showTime();
}
}
19. What is the output of the following code?
class A {
int i;
}
class B extends A {
int j;
void show() {
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class Inheritance {
public static void main(String args[]) {
B obj = new B();
obj.i = 6;
obj.j = 3;
obj.show();
}
}
20. Predict the correct results of below given code snippet:
abstract class B {
abstract void funTime();
}
class D extends B {
static void funTime() {
System.out.println("9");
}
}
class Main {
public static void main(String args[]) {
B object = new D();
object.funTime();
}
}
21. Find out the correct results of below given code snippet:
public class Main {
void process() {
String a = "82";
String b = subProcess(a);
System.out.print(" "+a + " " + b);
}
String subProcess(String a) {
a = a + " 128";
System.out.print(a);
return "292";
}
public static void main(String[] args) {
Main obj = new Main();
obj.process();
}
}
22. Predict the correct results of below given code snippet:
abstract class B {
B() {
System.out.println("7");
}
void funTime() {
System.out.println("72");
}
}
class D extends B {
D() {
System.out.println("139");
}
}
class Main {
public static void main(String args[]) {
D object = new D();
object.funTime();
}
}23. Predict the right output:
import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
String s= "03469";
System.out.println(s.length());
System.out.println(s.charAt(0));
String s1 = "034";
String s2 = "69";
System.out.println(s1.concat(s2));
String s4 = "14 21 37";
System.out.println(s4.indexOf("20"));
}
}
24. What is the output of the following code?
class A {
int i;
void show() {
System.out.println(i);
}
}
class B extends A {
int j;
void show() {
System.out.println(j);
}
}
class inheritanceDemo {
public static void main(String args[]){
B obj = new B();
obj.i = 1;
obj.j = 17;
obj.show();
}
}
25. Choose the correct output for the below given code snippet.
class CT {
static int temp = 37;
CT() {
temp = 70;
}
}
class Main {
public static void main(String args[]) {
CT t1 = new CT();
System.out.println(t1.temp*3);
}
}26. Predict the correct results of below given code snippet:
abstract class B {
abstract void funTime();
}
class D extends B {
final void funTime() {
System.out.println("31");
}
}
class Main {
public static void main(String args[]) {
B object = new D();
object.funTime();
}
}



0 Comments