Ticker

6/recent/ticker-posts

Java Programming Practice MCQs Codetantra. CBT-3

  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);
    }
}
  •  

    418

    418

    52

    52

  •  

    Error during unboxing of Character c to char d

  •  

    418

    418

    4

    4

  •  

    418

    418

    39999975

    39999975

    // Last two results are showing garbage values

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;
    }
}
  •  

    217 31

  •  

    31 168

  •  

    31 217

  •  

    168 31

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;
      }
   }
}
  •  

    {{5, 8, 3}, {2, 8, 6}}

  •  

    {{5, 8, 3},{2, 7, 5}}

  •  

    {{10, 16, 6}, {4, 7, 10}}

  •  

    {{5, 8, 3},{16, 7, 6}}

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;
    }
}
  •  

    2655 59

  •  

    59 630

  •  

    59 2655

  •  

    630 59

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); 
    }
}


  •  

    31063190

  •  

    3163190

  •  

    3106301900

  • 31630190

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();
    }
}
  •  

    -460

  •  

    885

    -460

  •  

    error: showTime() in Tantra cannot override showTime() in Code

  •  

    885

  •  

    -460

    885

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();
    }
} 
  • 169

  • 6

    169

  •  

    6

  • Error

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);
	}
}

  • 9

    2

  •  

    20

    1

  •  

    Error: error: getMethod() in Main cannot override getMethod() in D


  •  

    9

    1

  •  

    20

    2

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] + " "); 
  }
}
  • 9 28 3 9

  •  

    2 24 5 1

  •  

    2 9 13 16 

  • 24 28 31 39

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();
	}
}
  •  

    6896

    4323

  •  

    6896

    6896

  •  

    Error

  •  

    4323

    6896

  •  

    4323

    4323

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();
		}
	}
  •  

    7 14 7 14

  •  

    14 14

  •  

    7 14

  •  

    Compilation fails.

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);
	}
}
  •  

    19

    37

    68

    38

  •  

    19

    36

    68

    38

  •  

    19

    36

    68

    36

  •  

    error: cannot assign a value to final variable 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

    33

    65

    34

  •  

    15

    32

    65

    34

  •  

    error: interface abstract methods cannot have body


  •  

    15

    32

    65

    32

  •  

    error: cannot assign a value to final variable 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();   
   }
 }
  •  

    1 9

  •  

    1 15

  •  

    9 15

  •  

    Error

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);
	}
}
  • 114

    31

  •  

    114

  • Error: Violation in implementing interface a

  •  

    31

    114

  • 31

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();
	}
}


  •  

    124

    42

    57

    188

  • 124

    57

    42

    188

  •  

    Error: Violation in using static functions with abstract classes

  • 42

    124

    57

    188

  • 57

    124

    42

    188

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();
    }
}
  •  

    -197

  •  

    1016

    -197

  •  

    error: showTime() in Tantra cannot override showTime() in Code

  • 1016

  •  

    -197

    1016

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();   
    }
  }
  •  

    3 4

  •  

    3 3

  • 4 4

  • 4 3

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();
	}
}


  •  

    Error : overriding method is static

  •  

    Code executes without error without any output

  •  

    Clean code with 9 printed as output.

  •  

    Code gives warning due to use of static method , but also prints 9

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();
    }
}
  •  

    82 292 82 128

  •  

    82 82 128 292

  •  

    82 128 82 292

  • 128 292 82 82

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();
	}
}
  • 139

    7

    72

  •  

    139

    72

    7

  •  

    Error: Violating concept of abstract classes

  •  

    7

    139

    72

  •  

    72

    139

    7

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"));
	    
	}
}


  •  

    5

    9

    03469

    -1

  •  

    5

    0

    69034

    -1

  •  

    5

    0

    03469

    -1

  • Compile time error

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();   
    }
  }
  •  

    1

  •  

    1 17

  •  

    17

  •  

    17 1

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);
   }
}
  • 111

  •  

    0

  •  

    210

  •  

    377032101110 some garbage value

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();
	}
}

  • Error : overriding method is final

  •  

    Code executes without error without any output

  •  

    Clean code with 31 printed as output.

  •  

    Code gives warning due to use of final method , but also prints 31






Post a Comment

0 Comments