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); }
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); }
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?
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 ?
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?
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 ?
public class TestString1 { public static void main(String[] args) { String str = "137"; str += 205; System.out.print(str); } }
07. What is the output?
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?
public class Main { public static void main(String[] args){ int a = 6; System.out.println(a++++); } }
09. What is the output?
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 ?
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?
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
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
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?
public class Test { public static void main(String[] args){ int a = 20; System.out.println(a--*a--); } }
15. What is the output?
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.
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?
public class Solution { public static void main(String args[]) { int a = 15; System.out.print(++a * 47); } }
18. What is the output
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?
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] + " "); } }
21.
int nums1[] = new int[3]; int nums2[] = {6, 2, 8, 5, 2}; nums1 = nums2; for (int x : nums1) { System.out.print(x + ":"); }
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) ; } } }
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?
24.
public class Main { public static void main(String[] args){ int a = 19; System.out.println(++a*++a); } }
What is the output?
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"); } } }
0 Comments