Data Types, Variables and Arrays

1-Which of the following are legal lines of Java code?

1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;

a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct.


2-An expression involving byte, int, and literal numbers is promoted to which of these

a) int
b) long
c) byte
d) float


3-What is the output of this program?
  1.     class increment {

  2.         public static void main(String args[]) 

  3.         {        

  4.              int g = 3;

  5.              System.out.print(++g * 8);

  6.         } 

  7.     }

a) 25
b) 24
c) 32
d) 33


4-Which of these coding types is used for data type characters in Java?

a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned


5-Which one is a valid declaration of a boolean?


a) boolean b1 = 1;
b) boolean b2 = ‘false’;
c) boolean b3 = false;
d) boolean b4 = ‘true’


6-What is the output of this program?
  1.     class array_output {

  2.         public static void main(String args[]) 

  3.         {    

  4.             char array_variable [] = new char[10];

  5.      for (int i = 0; i < 10; ++i) {

  6.                 array_variable[i] = 'i';

  7.                 System.out.print(array_variable[i] + "" );

  8.                 i++;

  9.             }

  10.         } 

  11.     }

a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned


7-What is the output of this program?

 

  1.     class mainclass {

  2.         public static void main(String args[]) 

  3.         {

  4.             char a = 'A';

  5.             a++;

  6.      System.out.print((int)a);

  7.         } 

  8.     }

a) 66
b) 67
c) 65
d) 64


8-What is the output of this program?
  1.     class mainclass {

  2.         public static void main(String args[]) 

  3.         {

  4.             boolean var1 = true;

  5.      boolean var2 = false;

  6.      if (var1)

  7.          System.out.println(var1);

  8.      else

  9.          System.out.println(var2);

  10.        } 

  11.     }

a) 0
b) 1
c) true
d) false


9-What is the output of this program?
  1.     class booloperators {

  2.         public static void main(String args[]) 

  3.         {

  4.             boolean var1 = true;

  5.      boolean var2 = false;

  6.      System.out.println((var2 & var2));

  7.         } 

  8.     }

a) 0
b) 1
c) true
d) false


10-Which of these operators is used to allocate memory to array variable in Java?


a) malloc
b) alloc
c) new
d) new malloc


11-Which of these is an incorrect array declaration?


a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] arr = new int[5]
d) int arr[] = int [5] new


12-Which of these is an incorrect Statement?


a) It is necessary to use new operator to initialize an array.
b) Array can be initialized using comma separated expressions surrounded by curly braces.
c) Array can be initialized when they are declared.
d) None of the mentioned


13-Which of these is necessary to specify at time of array initialization?


a) Row
b) Column
c) Both Row and Column
d) None of the mentioned


14-What is the output of this program?

 

  1.    class array_output {

  2.         public static void main(String args[]) 

  3.         {

  4.             int array_variable [] = new int[10];

  5.      for (int i = 0; i < 10; ++i) {

  6.                 array_variable[i] = i;

  7.                 System.out.print(array_variable[i] + " ");

  8.                 i++;

  9.             }

  10.         } 

  11.     }

a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10


15-What is the output of this program?

 

  1.     class multidimention_array {

  2.         public static void main(String args[])

  3.         {

  4.             int arr[][] = new int[3][];

  5.             arr[0] = new int[1];

  6.             arr[1] = new int[2];

  7.             arr[2] = new int[3];               

  8.      int sum = 0;

  9.      for (int i = 0; i < 3; ++i) 

  10.          for (int j = 0; j < i + 1; ++j)

  11.                     arr[i][j] = j + 1;

  12.      for (int i = 0; i < 3; ++i) 

  13.          for (int j = 0; j < i + 1; ++j)

  14.                     sum + = arr[i][j];

  15.      System.out.print(sum);  

  16.         } 

  17.     }

a) 11
b) 10
c) 13
d) 14


16-What is the output of this program?

 

  1.     class evaluate {

  2.         public static void main(String args[])

  3.             {

  4.          int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};

  5.          int n = 6;

  6.                 n = arr[arr[n] / 2];

  7.          System.out.println(arr[n] / 2);

  8.             }

  9.     }

a) 3
b) 0
c) 6
d) 1


17- Which of these is necessary condition for automatic type conversion in Java?


a) The destination type is smaller than source type.
b) The destination type is larger than source type.
c) The destination type can be larger or smaller than source type.
d) None of the mentioned


18-What is the error in this code?


byte b = 50;
b = b * 50;
a) b can not contain value 100, limited by its range.
b) * operator has converted b * 50 into int, which can not be converted to byte without casting.
c) b can not contain value 50.
d) No error in this code


19-If an expression contains double, int, float, long, then whole expression will promoted into which of these data types?


a) long
b) int
c) double
d) float


20-What is Truncation is Java?


a) Floating-point value assigned to an integer type.
b) Integer value assigned to floating type.
c) Floating-point value assigned to an Floating type.
d) Integer value assigned to floating type.


21-What is the output of this program?

 

  1.     class conversion {

  2.         public static void main(String args[])

  3.         {

  4.             double a = 295.04;

  5.             int  b = 300;

  6.             byte c = (byte) a;

  7.             byte d = (byte) b;

  8.             System.out.println(c + " "  + d);

  9.         }

  10.     }

a) 38 43
b) 39 44
c) 295 300
d) 295.04 300


21-What is the output of this program?

 

  1.     class A {

  2.         final public int calculate(int a, int b) { return 1; } 

  3.     } 

  4.     class B extends A { 

  5.         public int calculate(int a, int b) { return 2; } 

  6.     } 

  7.      public class output {

  8.         public static void main(String args[]) 

  9.         { 

  10.             B object = new B(); 

  11.             System.out.print("b is " + b.calculate(0, 1));  

  12.         } 

  13.     }

a) b is : 2
b) b is : 1
c) Compilation Error.
d) An exception is thrown at runtime.


 22-What is the output of this program?

 

  1.     class c {    

  2.         public void main( String[] args ) 

  3.         {  

  4.             System.out.println( "Hello" + args[0] ); 

  5.         } 

  6.     }

a) Hello c
b) Hello
c) Hello world
d) Runtime Error.


Answer Key will be mailed on Request

Related Articles

post a comment