TCS ASPIRE : Java quiz/question and answers latest

# Question 1 of 10 10.0 Points
Among the following options, choose the four options that describe the appropriate default values for array elements of the types indicated? 
1. int -> 0 
2. String -> “null” 
3. Dog -> null 
4. char -> ‘\u0000′ 
5. float -> 0.0f 
6.boolean -> true 
A. 1, 2, 3, 4
B. 2, 4, 5, 6
C. 3, 4, 5, 6
D. 1, 3, 4, 5

Answer Key: D 
[Tips:(1), (3), (4), (5) are the correct statements. (2) is wrong because the default value for a String (and any other object reference) is null, with no quotes.(6)is wrong because the default value for boolean elements is false.] 
# Question 2 of 10 10.0 Points
Which option among the following lists only Java programming language keywords?
A. goto, instanceof, native, finally, default, throws
B. byte, break, assert, switch, include
C. try, virtual, throw, final, volatile, transient
D. strictfp, constant, super, implements, do
E. class, if, void, long, Int, continue

Answer Key: A 
 [Tips: All the words in goto, instanceof, native, finally, default, throws are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function. class, if, void, long, Int, continue is wrong because the keyword for the primitive int starts with a lowercase i. try, virtual, throw, final, volatile, transient is wrong because “virtual” is a keyword in C++, but not Java. strictfp, constant, super, implements, do is wrong because “constant” is not a keyword. Constants in Java are marked static and final. byte, break, assert, switch, include is wrong because “include” is a keyword in C, but not in Java.]
# Question 3 of 10 10.0 Points
Which of the following options would legally declare, construct, and initialize an array? 
A. int [] myList = {“1″, “2″, “3″};
B. int myList [] = {4, 3, 7};
C. int [] myList = (5, 8, 2);
D. int myList [] [] = {4,9,7,0};

Answer Key: B 
[Tips: The only legal array declaration and assignment statement is
int myList [] = {4, 3, 7}; int [] myList = {“1″, “2″, “3″}; is wrong because it initializes an int array with String literals. int [] myList = (5, 8, 2);is wrong because it use something other than curly braces for the initialization. int myList [] [] = {4,9,7,0}; is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array.] 
# Question 4 of 10 10.0 Points
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
 A. private
B. transient
C. public
D. protected

Answer Key: C 
[Tips:Access modifiers dictate which classes, not which instances, may access features. Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way. private makes a member accessible only from within its own class protected makes a member accessible only to classes in the same package or subclass of the class default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package. public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible) final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised abstract declares a method that has not been implemented. transient indicates that a variable is not part of the persistent state of an object. volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is protected. public is also a contender but protected is more restrictive, private would be the answer if the constraint was the “same package” instead of “any package” in other words the subclasses clause in the question eliminates default.]
# Question 5 of 10 10.0 Points
interface Base { boolean m1 (); byte m2(short s); } 
1.interface Base2 implements Base {} 
2.abstract class Class2 extends Base { public boolean m1(){ return true; }} 
3.abstract class Class2 implements Base {} 
4.abstract class Class2 implements Base { public boolean m1(){ return (7 > 4); }} 
5.abstract class Class2 implements Base { protected boolean m1(){ return (5 > 7) }} 
A. 3 and 4
B. 1 and 5
C. 1 and 2
D. 2 and 3

Answer Key: A 
[Tips: 3) is correct because an abstract class doesn’t have to implement any or all of its interface’s methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean). (1) is incorrect because interfaces don’t implement anything. (2) is incorrect because classes don’t extend interfaces. (5) is incorrect because interface methods are implicitly public, so the methods being implemented must be public.]
# Question 6 of 10 10.0 Points
Among the 6 options provided below, which three form part of correct array declarations?
1.public int a [ ]
2.static int [ ] a
3.public [ ] int a
4.private int a [3]
5.private int [3] a [ ]
6.public final int [ ] a
A. 2, 4, 5
B. 1, 3, 4
C. 2, 5, 6
D. 1, 2, 6


Answer Key: D 
[Tips: (1), (2) and (6) are valid array declarations. Option (3) is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a Option (4) is not a correct array declaration. The compiler complains with: ‘]’ expected. A closing bracket is expected in place of the 3. The following works: private int a [] Option (5) is not a correct array declaration. The compiler complains with 2 errors: ‘]’ expected. A closing bracket is expected in place of the 3 and expected A variable name is expected after a[ ] .]
# Question 7 of 10 10.0 Points
Which of the following is/are legal method declarations?
1.protected abstract void m1();
2.static final void m1(){}
3.synchronized public final void m1() {}
4.private native void m1();
A. All of them are legal declarations.
B. 1 and 3
C. 2 and 4
D. 1 only


Answer Key: A
[ Tips: All the given statements are legal declarations.]
# Question 8 of 10 10.0 Points
Which three among the following are valid method signatures in an interface? 
1.private int getArea(); 
2.public float getVol(float x); 
3.public void main(String [] args); 
4.public static void main(String [] args); 
5.boolean setFlag(Boolean [] test); 
A. 3, 4, and 5
B. 2 and 4
C. 1 and 2
D. 2, 3 and 5

Answer Key: D 
[ Tips: (2), (3), and (5). These are all valid interface method signatures. (1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.]
# Question 9 of 10 10.0 Points
Which option among the following creates an instance of an array?
A. int[ ] ia = new int[15];
B. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };
C. char[ ] ca = “Some String”;
D. float fa = new float[20];

Answer Key: A 
[ Tips: int[ ] ia = new int[15]; is correct. It uses correct array declaration and correct array construction. float fa = new float[20]; is incorrect. It generates a compiler error: incompatible types because the array variable declaration is not correct. The array construction expects a reference type, but it is supplied with a primitive type in the declaration. char[ ] ca = “Some String”; is incorrect. It generates a compiler error: incompatible types because a string literal is not assignable to a character type variable. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 }; is wrong, it generates a compiler error expected. The compiler thinks that you are trying to create two arrays because there are two array reinitialize to the right of the equals, whereas your intention was to create a 3 x 3 two-dimensional array.]
# Question 10 of 10 10.0 Points
Which of the following class level (nonlocal) variable declarations will not compile?
 A. volatile int d;
B. private synchronized int e;
C. protected int a;
D. transient int b = 3;

Answer Key: B
[Tips:private synchronized int e; will not compile; the synchronized modifier applies only to methods. protected int a; and transient int b = 3; will compile because protected and transient are legal variable modifiers. volatile int d; will compile because volatile is a proper variable modifier.]



IF you have More Question Share in Comment

1 comment: