Test 1 Variable Datatypes Arrays Literals
Test 1 Variable Datatypes Arrays Literals
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. }
CoreJavaAdvancedJava
TestCoreJavaAdvancedJava
CoreJava
(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?
1020
(Correct)
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. }
ab
195
(Correct)
Secure
Robustness
(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. }
1020
ab
(Correct)
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)
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?
(Correct)
(Correct)
int[][] x = new int[][2];
20
Compilation Fails.
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. }
CoreJava
TestCoreJavaAdvancedJava
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}};
All of these.
(Correct)
[Ljava.lang.String;@7852e922
null
(Correct)
null
null
10...20
40...20
(Incorrect)
30...30
(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}};
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 ?
(Correct)
(Correct)
(Correct)
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. }
false false
false true
(Correct)
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?
(Correct)
(Correct)
Compilation Fails.
195
(Correct)
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 ?
(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?
(Correct)
None of these.
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. }
...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. }
12 21
(Correct)
12 22
11 21
false false
(Incorrect)
true true
(Correct)
(Correct)
(Correct)
Test
Core
(Correct)
Core Java
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.
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. }
21.0
24.0
(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. }
20
(Correct)
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. }
(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 :
(Incorrect)
(Correct)