Ticker

6/recent/ticker-posts

Java Programming Practice MCQs Codetantra. CBT-2

 Java Programming Practice MCQs Codetantra. CBT-2

L1 to L31


01. Given a sequence of 10 digits as String


String text = "4174163964";


What is the output of the below code snippet:


if (text.length() > 10) {
    text = text.substring(3, 6) + text.substring(2, 5);
    System.out.println(text);
} else {  
    System.out.println(text);
}

  •  
    741174
    
  •  
    174417
    
  •  
    34562345
    
  •  
    4174163964
    
  •  
    74161741

02. Select the correct output:

public class Main { 
  public static void main(String args[]) { 
    String str = "72418";

    String[] arr1 = str.split("2");
    String[] arr2 = str.split("1");
  
    for (String s : arr1) 
      System.out.println(s); 
    for (String s : arr2) 
      System.out.println(s); 
  } 

  •  
    7
    8
    418
    724
    


  •  
    72
    418
    7241
    8
    


  •  
    7
    418
    724
    8
    
  •  
    7
    4
    1
    8
    7
    2
    4
    8
import java.util.*;
import java.lang.*;
public class Main{
    public static void main(String[] args) {
        int Info = 90; // Line 1
		String S = Integer.toString(Info); //Line 2
		String Temp = "19"; // Line 3
		int Data = Integer.parseInt(Temp); //Line 4
		System.out.println(Data + S);//Line 5


	}
    
}

03. What is the output?

  •  

    109

  •  

    9019

  •  

    1990

  •  

    Compilation Error due to error in Line 5

public class Pass {
	public static void main(String[] args) {
		int x = 251;
		Pass p = new Pass();
		p.doStuff(x);
		System.out.print(" main x = " + x);
	}

	void doStuff(int x) {
		System.out.print(" doStuff x = " + x++);
	}
}

04. What is the result ?

  • doStuff x = 252 main x = 251

  • doStuff x = 252 main x = 252

  • doStuff x = 251 main x = 252

  • doStuff x = 251 main x = 251

public class Batman {
	int squares = 547;
	public static void main(String[] args) {
		new Batman().go();
	}
	void go() {
		incr(++squares);
		System.out.println(squares);
	}
	void incr(int squares) {
		squares += 10;
	}
}

05. What is the result?

  •  

    548

  •  

    557

  •  

    547

  •  

    558

06. What will be the result of the expression a % b when a and b is of type int and their values are


a = 1114 and b= 200 ?

  • 114

  • 1314

  • 5

  • -914

public class TestString1 {
	public static void main(String[] args) {
		String str = "137";
		str += 205;
		System.out.print(str);
	}
}

07. What is the output?

  •  

    137

  •  

    342

  •  

    205

  •  

    137205

public class Test {
	public static void main(String[] args) {
		int a = 23;
		System.out.print(doCalc(a));
		System.out.print(" " + a);
	}

	static int doCalc(int a) {
		a = a * 55;
		return a;
	}
}
08.What is the result?
  • 23 1265

  • 23 55

  •  

    1265 23

  •  

    Compilation fails.

public class Main {
  public static void main(String[] args){
  int a = 6;
 System.out.println(a++++);
  }
}


09. What is the output?

  •  

    7

  •  

    6

  •  

    8

  •  

    Compilation Error

Given the code fragment :


public static void main(String[] args) {
	Short s1 = 5;
	Integer s2 = 2;
	Long s3 = (long) (s1 + s2); // line n1
	String s4 = (String) (s3 * s2); // line n2
	System.out.println("Sum is " + s4);
}


10.what is the result ?

  •  
    Compilation fails at line n1.
    
  •  
    Compilation fails at line n2.
    
  •  
    Sum is 14
    
  •  
    A ClassCastException is thrown at line n2.
    
  •  
    Sum is 7 
    
  •  
    A ClassCastException is thrown at line n1.

Given: the code fragment:

public class Test {
	public static void main(String[] args) {
		int[] array = new int[2];
		array[0] = 42;
		array[1] = 7;
		System.out.println(array[0] + array[1]);
	}
}


11. What is the result?

  •  

    427

  •  

    Error

  •  

    49

  •  

    742

Consider the following code:

int x,y,z;
y=99; z=63;
X=0-(++y)+z++;


12. What will be the values of X, y and z

  •  

    X = -37, y = 100, and z = 64

  •  

    X = -37, y = 99, and z = 64

  •  

    X = -36, y = 100, and z = 64

  •  

    X = -37, y = 100, and z = 63

13. Choose the correct answer for the expression:


boolean status = ((9104/55)>197) || ((8391/48)>125);


Note: In output 0 is false and 1 is treated as true

  •  

    1

  •  

    Compile time error

  •  

    0174111650 some garbage value

  •  

    0

  •  

    Runtime error

public class Test {
  public static void main(String[] args) {
    int a = 5 + 5 * 3 + 3 * 3 + (3 * 8);
    System.out.println(a);
  }
}


14. What is the output?

  •  

    124

  •  

    407 

  • 53

  •  

    None

public class Test {
  public static void main(String[] args){
  int a = 20;
  System.out.println(a--*a--);
  }
}


15. What is the output?

  •  

    380

  •  

    361

  •  

    400

  • 7998

16. You are given an array arr of elements is given = 462,398,234,169,6 .

What are the steps of insertions done while performing insertion sort in the array?

Note: Order of sorting is in ascending order.

  •  

    Step 1: 462 398 234 6 169

    Step 2: 462 398 6 169 234

    Step 3: 462 6 169 234 398

    Step 4: 6 169 234 398 462

  •  

    Step 1: 398 234 169 6 462

    Step 2: 234 169 6 462 398

    Step 3: 169 6 462 398 234

    Step 4: 6 169 234 398 462

  •  

    Step 1: 398 462 234 169 6

    Step 2: 234 398 462 169 6

    Step 3: 169 234 398 462 6

    Step 4: 6 169 234 398 462

  •  

    Step 1: 398 462 234 169 6

    Step 2: 169 234 398 462 6

    Step 3: 234 398 462 169 6

    Step 4: 6 169 234 398 462

public class Main {
    public static void main(String[] args) {
        double Data = 663.46;
        int Info = Data;
        System.out.println(Data);
        }
    
}


17. What is the output of above given code snippet?

  •  

    663

  •  

    Run time error due to Lossy conversion

  •  

    663.46

  •  

    Compile time error due to Lossy conversion

public class Solution {
    public static void main(String args[]) 
    {     
       int a = 15;
       System.out.print(++a * 47);
    } 
  }


18. What is the output

  • 735

  • 120

  • 752

  •  

    None

Given:

class Test {
    static int i;
    int j;


    public static void main(String[] args) {
	Test x1 = new Test();
	Test x2 = new Test();
	x1.i = 30;
	x1.j = 94;
	x2.i = 58;
	x2.j = 23;
	System.out.println(x1.i + " " + x1.j + " " + x2.i + " " + x2.j);
    }
}



19. What is the result?

  •  

    30 94 58 23

  • 30 23 94 23

  • 58 94 58 23

  •  

    30 94 30 23

20. 
public class Test 
  {
    public static void main(String args[])
    {
      int arr[] = {82, 50, 18, 76, 43};
      for ( int i = 0; i < arr.length - 2; ++i)
        System.out.print(arr[i] + " ");
    } 
  }
  • 82 50 18 76

  •  

    82 50

  •  

    82 50 18 76 43

  • 82 50 18

21.
int nums1[] = new int[3];
int nums2[] = {6, 2, 8, 5, 2};
nums1 = nums2;
for (int x : nums1) {
     System.out.print(x + ":");
}


  •  

    6:2:8:

  •  

    Compilation fails.

  •  

    6:2:8:5:2:

  •  

    An ArrayoutofBoundsException is thrown at runtime.

22. What will be the output of below given code snippet?

public class Question {

   public static void main (String [ ] args) {

           int var = 69, anotherVar = 33, result ;

           String str = "97" ;

           String anotherStr = "62" ;

           result = var*anotherVar / anotherVar ;

           if ( result < 13 ) {

                System.out.println(str) ;

           }

           else {

                     System.out.println(anotherStr) ;

           }

    }

}



  •  

    33

  • Compilation error: incorrect use of operators

  •  

    97

  •  

    No output

23. 

int[] intArr = {36, 94, 62, 30, 87};

intArr[2] = intArr[4];

intArr[4] = 55;


What are the values of each element in intArr after this code has executed?

  •  

    3655625587

  •  

    3694873055

  • 3630625587

  •  

    3694553055

24.
public class Main {
  public static void main(String[] args){
  int a = 19;
  System.out.println(++a*++a);
  }
}



What is the output?

  •  

    380

  •  

    420

  •  

    361

  •  

    400

25. What will be the output of below given code snippet?

public class Main {
  public static void main (String args[]) {

    int x = 0;

    if (x) {

      System.out.println ("58");

    }

    else {

      System.out.println ("22");

    }

  }

}

  •  

    22

  •  

    5822

  •  

    58

  •  

    Compile time error




Post a Comment

0 Comments