0% found this document useful (0 votes)
103 views27 pages

Test 1 Variable Datatypes Arrays Literals

The document contains 15 multiple choice questions about Java programming concepts like arrays, strings, default values, scope, and two-dimensional arrays. The questions test understanding of syntax, valid declarations, expected output, and exceptions. Correct answers are provided with brief explanations.

Uploaded by

Shaik Kalil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
103 views27 pages

Test 1 Variable Datatypes Arrays Literals

The document contains 15 multiple choice questions about Java programming concepts like arrays, strings, default values, scope, and two-dimensional arrays. The questions test understanding of syntax, valid declarations, expected output, and exceptions. Correct answers are provided with brief explanations.

Uploaded by

Shaik Kalil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 27

Question 1: 

Correct
Consider the array:

int[] x = {10,20,30,40};

Which of the following represents the number of elements present inside array?

x.size()

x.length

(Correct)

x.length()

x.size
Explanation
By using length variable, we can find number of elements present inside array.
Question 2: Correct
Consider the following code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. for(int i =0; i<= args.length; i++)
6. {
7. System.out.print(args[i]);
8. }
9. }
10. }

and the command invocation

java Test Core Java Advanced Java


What is the result?

CoreJavaAdvancedJava

TestCoreJavaAdvancedJava

CoreJava

An exception is thrown at runtime.

(Correct)

Explanation
args[0]==>Core
args[1]==>Java
args[2]==>Advanced
args[3]==>Java
In the for loop, we are trying to print args[4] which is not available and hence we will
get ArrayIndexOutOfBoundsException.
CoreJavaAdvancedJavaException in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 4
at Test.main(Test.java:7)
Question 3: Correct
Consider the following code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. int[] a = {10,20,30};
6. int[] b = {10,20};
7. a=b;
8. for(int i :a)
9. {
10. System.out.print(i);
11. }
12. }
13. }
What is the result?

The Compilation Fails.

1020

(Correct)

An exception is thrown at runtime.

102030
Explanation
Whenever we are assigning one array to another array, internal elements won't be
copied, just reference variables will be reassigned. Hence the sizes are not required
to match. But types must be matched. In the above code, after a=b, both a and b are
pointing to {10,20}. Hence the output is: 1020.
Question 4: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. int[] a = new int[3];
6. a[0]='a';
7. a[1]='b';
8. System.out.println(a[0]+a[1]+a[2]);
9. }
10. }

What is the result ?

ab


195

(Correct)

An exception is thrown at runtime.

The Compilation Fails.


Explanation
Once we creates an array, every array element will be initialized with default values.
for int[] array, it is 0.
In the place of int, we can provide char and its unicode value will be considered.
Hence a[0]+a[1]+a[2] means 97+98+0 which is 195.
Question 5: Correct
Java applications can run on various devices and computers without performing any
modifications. This feature is called

Secure

Robustness

Platform independent Nature

(Correct)

Multi-threaded
Explanation
Once we write a java program, we can run on any platform with out performing any
changes. This behaviour is called platform independent nature of java.
Question 6: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. int[] a = {10,20};
6. char[] b = {'a','b'};
7. a=b;
8. for(int i :a)
9. {
10. System.out.print(i);
11. }
12. }
13. }

What is the result?

1020

ab

The Compilation Fails.

(Correct)

An exception is thrown at runtime.


Explanation
There is no compatibility between char[] object and int[] object. Hence we cannot
assign char[] object with int[] type reference variable.
In the above code, we are trying to assign char[] object with int[] type reference
variable. Hence we will get compile time error.
Test.java:7: error: incompatible types: char[] cannot be converted to int[]
Question 7: Correct
Consider the following statements :

S1: Once we compiled java source file, it can run on any platform where JRE is
available. This feature is called Write Once and Run Anywhere(WORA)

S2: Java is platform independent where as JVM is platform dependent.


Which of the above statements are TRUE?

Both S1 and S2

(Correct)

Only S2

Only S1

None S1 and S2
Explanation
Once we write a java program and compiled, it can run on any platform where JRE is
available. This feature is called Write Once and Run Anywhere(WORA) policy of
java.Java is platform independent where as JVM is platform dependent.
Question 8: Correct
Which of the following are valid array declarations?

int[][] x = new int[2][];

(Correct)

int[][] x = new int[2][2];

(Correct)


int[][] x = new int[][2];

int[][] x = new int[][];


Explanation
When ever we are creating multi dimensional array, atlease base dimension size, we
have to specify. Without specifying first dimension size, we cannot specify second
dimension size.
Question 9: Correct
Consider the code:
1. public class Test
2. {
3. public static void m1(int x)
4. {
5. x=20;
6. }
7. public static void main(String[] args)
8. {
9. int x =10;
10. m1(x);
11. System.out.println(x);
12. }
13. }

What is the result?

20

Compilation Fails.

An exception is thrown at runtime.

10

(Correct)

Explanation
If we pass any primitive to a method as argument, and within the method if we are
performing any changes to the primitive value, those changes won' be reflected to
the caller. In this case duplicate copy of that primitive value will be maintained.
Question 10: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. String[] argv = new String[2];
6. argv=args;
7. for(String v : argv)
8. {
9. System.out.print(v);
10. }
11. }
12. }

and the command invocation

java Test Core Java Advanced Java

What is the result?

CoreJava

TestCoreJavaAdvancedJava

An exception is thrown at runtime.

CoreJavaAdvancedJava

(Correct)

Explanation
Whenever we are assigning one array to another array internal elements won't be
copied. Just reference variables will be reassigned. Hence sizes are not required to
match.
In the above code, after the following line,
argv=args;
both references are pointing to the command-line arguments array only. for loop just
prints all elements present in that array. Hence the output is:
CoreJavaAdvancedJava
Question 11: Correct
Which of the following are valid two-dimensional array declarations ?

int[][] a = {{10,20},{30,40}};

int[][] a = new int[2][2];

All of these.

(Correct)

int[][] a = new int[][] {{10,20},{30,40}};


Explanation
All these are valid two-dimensional array declarations.
int[][] a = new int[2][2];==>In this case all array elements will be initialized with default
values. But in the remaining two cases, elements will be initialized with provided
values.
Question 12: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. String[] s = new String[2];
6. System.out.println(s);
7. System.out.println(s[0]);
8. }
9. }

What is the result ?


[Ljava.lang.String;@7852e922

null

(Correct)

null

null

An exception thrown at runtime.

The compilation fails.


Explanation
Whenever we are trying to print array reference, internally toString() method will be
called, which prints the output in the following format:
classname@hashcode_in_hexadecimalstringform
Once we creates String[], element element is assigned with default value which is
null.
Hence the output is:
[Ljava.lang.String;@7852e922
null
Question 13: Incorrect
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. int x =10;
6. String s= "Java";
7. if(s.equals("Java"))
8. {
9. int y = 20;
10. x= x+30;
11. }
12. System.out.println(x+"..."+y);
13. }
14. }

What is the result?


10...20

40...20

(Incorrect)

30...30

The compilation fails.

(Correct)

Explanation
y is the local variable of if block and cannot be used outside of the block. Hence we
will get compile time error.
Question 14: Incorrect
Consider the following declarations:
1. 1. String[][] s = new String[2][1];
2. 2. String[] s[] = new String[2][1];
3. 3. String[][] s = {{null},{null}};
4. 4. String[][] s = {{null, null}};

Which of the above declarations are equal syntactically ?

1,2,4

(Incorrect)

1,2,3
(Correct)

1,2

2,3
Explanation
Once we creates an array, every element will be assigned with default values. Hence
the following 3 declarations are equal syntactically.
String[][] s = new String[2][1];
String[] s[] = new String[2][1];
String[][] s = {{null},{null}};
Question 15: Correct
Which of the following are TRUE ?

For int type variables, the default value is 0.

(Correct)

For char type variables, the default value is null.

For String type variables, the default value is null.

(Correct)

For double type variables, the default value is 0.0.

(Correct)

For boolean type variables, the default value is true.


Explanation
For boolean type variables, the default value is false.
For char type variables, the default value is space character.
For int type variables, the default value is 0.
For String type variables, the default value is null.
For double type variables, the default value is 0.0.
Question 16: Correct
Consider the code:
1. class Demo
2. {
3. String title ="Demo";
4. }
5. public class Test
6. {
7. public static void m1(Demo d)
8. {
9. d.title = "NewDemo";
10. }
11. public static void main(String[] args)
12. {
13. Demo d =new Demo();
14. m1(d);
15. System.out.println(d.title);
16. }
17. }

What is the result ?

An exception is thrown at runtime.

Demo

NewDemo

(Correct)


Compilation Fails.
Explanation
If we pass an object reference to a method as argument, and within the method if we
are performing any changes to the state of object, those changes will be reflected to
the caller. In this case just duplicate reference variable will be created but not
duplicate object.
Question 17: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. Boolean b1 = new Boolean(null);
6. Boolean b2 = new Boolean(false);
7. System.out.print((b1 == b2)+" ");
8. System.out.print(b1.equals(b2));
9. }
10. }

What is the result ?

false false

An exception is thrown at runtime.

false true

(Correct)

The Compilation Fails.


Explanation
When ever we are using new operator, always a new object will be created in the
heap. For Boolean constructor, if we pass null then it will be considered as false.
Hence in the above code both b1 and b2 represents false but different objects. In all
wrapper classes including Boolean, equals() method is overridden for content
comparison. Hence,
b1==b2====>returns false
b1.equals(b2)=====>returns true.
Question 18: Correct
The variables which are declared inside a method are considered as

class variables

instance variables

static variables

local variables

(Correct)

Explanation
The variables which are declared inside a method are considered as local variables.
Question 19: Correct
Which of the following are True?

All of the above.

Java code compiled on Linux can run on windows.

(Correct)

Java is Object Oriented Programming language.

(Correct)

Java is Procedural Programming language.


Explanation
Java is Object Oriented Programming language but not Procedural Programming
language. Java code compiled on one platform can run on any platform. Hence Java
is considered as platform independent.
Question 20: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. char ch1 = 'a';
6. char ch2 = 'b';
7. System.out.println(ch1+ch2);
8. }
9. }

What is the result?

Compilation Fails.

195

(Correct)

An exception is thrown at runtime.

ab
Explanation
Whenever we are applying + operator between two char types, then the result is
always int type. The corresponding unicode values(for 'a' it is 97 and for 'b' it is 98)
will be considered. Hence the output is : 195.
Question 21: Correct
Which of the following declarations are valid ?

float f =(float) 10.5;

(Correct)

float f = 10.5;

int i = 10.5;

double d = 10.5;

(Correct)

Explanation
Every floating point literal is by default of double type. Hence we cannot assign to int
and float variables directly.
Question 22: Correct
Which of the following are valid array declarations?

Object[] o = new String[10];

(Correct)

None of these.

int[] x =  new char[10];


double[] d = new float[10];
Explanation
In the case of primitive arrays, we cannot assign one array to another array. But in
the case of object type arrays, we can assign child class type array object with
parent class type array reference. Hence the following declaration is valid.
Object[] o = new String[10];
Question 23: Correct
Which of the following are valid declarations?

double d =123.4_5_6;

(Correct)

double d =123.456;

(Correct)

double d =123.456_;

double d =123._456;
Explanation
We should use _ symbol between digits only. Hence the following are invalid.
double d =123._456;
double d =123.456_;
Question 24: Correct
Consider the code:
1. public class Student
2. {
3. private String name;
4. private boolean passed;
5. private int marks;
6.  
7. public static void main(String[] args)
8. {
9. Student s = new Student();
10. System.out.println(s.name+"..."+s.passed+"..."+s.marks);
11. }
12. }

What is the result ?

The Compilation Fails

...false...0

null...true...0

null...false...0

(Correct)

Explanation
Once we creates an object, every instance variable will be initialized with default
values. The default value for String is null, for boolean is false and for int is 0.
Question 25: Correct
Consider the following code:
1. public class Test
2. {
3. static int i =10;
4. int j = 20;
5. public void m1()
6. {
7. i++;
8. j++;
9. }
10. public static void main(String[] args)
11. {
12. Test t1 = new Test();
13. t1.m1();
14.  
15. Test t2 = new Test();
16. t2.m1();
17.  
18. System.out.print(t2.i+" "+t2.j);
19. }
20. }

What is the result?


12 21

(Correct)

12 22

11 21

Compilation Fails as we are accessing static variable from instance method.


Explanation
In the case of instance variable, for every object a separate copy will be created, but
only one copy of static variable will be created which is shared by all objects of that
class. In the above code instance variable will be incremented only once per object,
but static variable incremented for both objects. Hence the output is: 12 21
Question 26: Incorrect
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. Boolean b1 = Boolean.valueOf(null);
6. Boolean b2 = Boolean.valueOf(false);
7. System.out.print((b1 == b2)+" ");
8. System.out.print(b1.equals(b2));
9. }
10. }

What is the result ?

false false

(Incorrect)

true true

(Correct)

The Compilation Fails.

An exception is thrown at runtime.


Explanation
valueOf() methods internally uses autoboxing concept where object reusability is
there. For Boolean valuOf(), if we pass null then it will be considered as false.
Hence in the above code both b1 and b2 represents false and pointing to the same
object. In all wrapper classes including Boolean, equals() method is overridden for
content comparison. Hence,
b1==b2====>returns true.
b1.equals(b2)=====>returns true.
Question 27: Incorrect
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. char c1 = 'a';//Line-1
6. char c2 = 97;//Line-2
7. char c3 = 65536;//Line-3
8. char c4 = -10;//Line-4
9. System.out.println(c1+c2+c3+c4);
10. }
11. }

Which of the following are TRUE ?

Compilation Fails at Line-4.

(Correct)

An exception is thrown at runtime.

Compilation Fails at Line-3.

(Correct)

Compilation Fails at Line-2.

It prints 65720 to the console.

Compilation Fails at Line-1.


Explanation
The only allowed range of values for char data type is: 0 to 65535. Hence 65536 and
-10 are not allowed for char type. Due to this we will get compile time error at Line-3
and Line-4.
Test.java:7: error: incompatible types: possible lossy conversion from int to char char
c3 = 65536;//Line-3
Test.java:8: error: incompatible types: possible lossy conversion from int to char char
c4 = -10;//Line-4
Question 28: Correct
Consider the following code:
1. public class Test
2. {
3. public static void main(String... subjects)
4. {
5. System.out.println(subjects[0]);
6. }
7. }

and the command invocation

java Test Core Java Advanced Java

What is the result?


Test

Core

(Correct)

Core Java

An exception is thrown at runtime.


Explanation
class file name won't be included in command line arguments list. Space is the
separator between command line arguments. java Test args[0] args[1] args[2] Hence
the output is: Core
Question 29: Correct
Consider the statements :

S1: If we perform any changes to primitive values which are passed to a method,
those changes will be reflected to the caller method.

S2: If we perform any changes to instance variables of Objects which are passed to a
method, those changes will be reflected to the caller method.

Which of the above statements are valid ?

Both S1 and S2.

None of  S1 and S2.


Only S2.

(Correct)

Only S1.
Explanation
In the case of primitive values, a separate copy will be created and hence changes
won't be reflected to the caller. But in the case of Object reference, a new reference
variable will be created which is pointing to the same object and hence changes will
be reflected to the caller.
Question 30: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. int a =10;
6. double b = 10.0;
7. boolean c= true;
8. System.out.println(++a + ++b + ++c);
9. }
10. }

What is the result?

21.0

24.0

An exception is thrown at runtime.

The Compilation Fails.

(Correct)
Explanation
increment and decrement operators are applicable for all primitive data types except
boolean. In the above code we are trying to apply for boolean type and hence we will
get compile time error.
error: bad operand type boolean for unary operator '++'
System.out.println(++a + ++b + ++c);
Question 31: Correct
Consider the code:
1. public class Test
2. {
3. public static void main(String[] args)
4. {
5. int[] a = new int[3];
6. a[0]=10;
7. a[1]=10.0;
8. System.out.println(a[0]+a[1]+a[2]);
9. }
10. }

What is the result?

20

The Compilation Fails.

(Correct)

An exception is thrown at runtime.

20.0
Explanation
For the int type array, we cannot provide double value. Hence we will get compile
time error.
Test.java:7: error: incompatible types: possible lossy conversion from double to int
a[1]=10.0;
Question 32: Correct
Consider the code:
1. public class Test
2. {
3. int count;
4. public void Test()
5. {
6. count=7;
7. }
8. public static void main(String[] args)
9. {
10. Test t = new Test();
11. System.out.println(t.count);
12. }
13. }

What is the result ?

An exception is thrown at runtime.

The Compilation Fails.

(Correct)

Explanation
In the above example, observe that
public void Test(){...}
It is not constructor and it is a method. Hence instance variable has only default
value 0.
Question 33: Incorrect
Consider the following statements :

S1: For instance variables JVM will provide default values.

S2: For static variables JVM will provide default values.

S3: For local variables JVM will provide default values.


Which of the following are True?

Only S2 and S3.

Only S1 and S3.

(Incorrect)

Only S1 and S2.

(Correct)

All S1,S2 and S3.


Explanation
For instance and static variables JVM will provide default values and we are not
required to perform initialization explicitly. But for local variables, JVM won't provide
any default values and we have to perform initialization explicitly before using that
variable.

You might also like