Sail E0 Webinar

MCQs

Total Questions : 21 | Page 2 of 3 pages
Question 11.

Which three are valid declarations of a float?

  1. float f1 = -343;
  2. float f2 = 3.14;
  3. float f3 = 0x12345;
  4. float f4 = 42e7;
  5. float f5 = 2001.0D;
  6. float f6 = 2.81F;


  1.    1, 2, 4
  2.    2, 3, 5
  3.    1, 3, 6
  4.    2, 4, 6
 Discuss Question
Answer: Option C. -> 1, 3, 6

(1) and (3) are integer literals (32 bits), and integers can be legally assigned to

 floats (also 32 bits). (6) is correct because (F) is appended to the literal, 

declaring it as a float rather than a double (the default for floating point literals).

(2), (4),and (5) are all doubles.



Question 12.

Which is the valid declarations within an interface definition?


  1.    public double methoda();
  2.    public final double methoda();
  3.    static void methoda(double d1);
  4.    protected void methoda(double d1);
 Discuss Question
Answer: Option A. -> public double methoda();

Option A is correct. A public access modifier is acceptable. The method prototypes in 

an interface are all abstract by virtue of their declaration, and should not be declared 

abstract.

Option B is wrong. The final modifier means that this method cannot be constructed in 

a subclass. A finalmethod cannot be abstract.

Option C is wrong. static is concerned with the class and not an instance.

Option D is wrong. protected is not permitted when declaring a method of an interface. 

See information below.

Member declarations in an interface disallow the use of some declaration modifiers; 

cannot use transient, volatile, or synchronized in a member declaration in an inter 

you ace. Also, you may not use the private and protected specifiers when declaring 

members of an interface.

Question 13.

Which three are valid declarations of a char?

  1. char c1 = 064770;
  2. char c2 = 'face';
  3. char c3 = 0xbeef;
  4. char c4 = u0022;
  5. char c5 = 'iface';
  6. char c6 = 'uface';


  1.    1, 2, 4
  2.    1, 3, 6
  3.    3, 5
  4.    5 only
 Discuss Question
Answer: Option B. -> 1, 3, 6

(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the 

integer value 27128, which is legal because it fits into an unsigned 16-bit integer. 

char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, 

which fits into an unsigned 16-bit integer. char c6 = 'uface'; is a Unicode 

representation of a character.

char c2 = 'face'; is wrong because you can't put more than one character in a 

char literal. The only other acceptable char literal that can go between single quotes

 is a Unicode value, and Unicode literals must always start with a 'u'.

char c4 = u0022; is wrong because the single quotes are missing.

char c5 = 'iface'; is wrong because it appears to be a Unicode representation 

(notice the backslash), but starts with 'i' rather than 'u'.


Question 14.

Which one is a valid declaration of a boolean?


  1.    boolean b1 = 0;
  2.    boolean b2 = 'false';
  3.    boolean b3 = false;
  4.    boolean b4 = Boolean.false();
  5.    boolean b5 = no;
 Discuss Question
Answer: Option C. -> boolean b3 = false;

A boolean can only be assigned the literal true or false.


Question 15.

Which one of the following will declare an array and initialize it with five numbers?


  1.    Array a = new Array(5);
  2.    int [] a = {23,22,21,20,19};
  3.    int a [] = new int[5];
  4.    int [5] array;
 Discuss Question
Answer: Option B. -> int [] a = {23,22,21,20,19};

Option B is the legal way to declare and initialize an array with five elements.

Option A is wrong because it shows an example of instantiating a class

 named Array, passing the integer value 5 to the object's constructor. 

If you don't see the brackets, you can be certain there is no actual array 

object! In other words, an Array object (instance of class Array) is not the

 same as an array object.

Option C is wrong because it shows a legal array declaration, but with no 

initialization.

Option D is wrong (and will not compile) because it declares an array with a 

size. Arrays must never be given a size when declared.



Question 16.

Which one of these lists contains only Java programming language keywords?


  1.    class, if, void, long, Int, continue
  2.    goto, instanceof, native, finally, default, throws
  3.    try, virtual, throw, final, volatile, transient
  4.    strictfp, constant, super, implements, do
  5.    byte, break, assert, switch, include
 Discuss Question
Answer: Option B. -> goto, instanceof, native, finally, default, throws

All the words in option B are among the 49 Java keywords. Although goto reserved as a 

keyword in Java, goto is not used and has no function.

Option A is wrong because the keyword for the primitive int starts with a lowercase i.

Option C is wrong because "virtual" is a keyword in C++, but not Java.

Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.

Option E is wrong because "include" is a keyword in C, but not in Java.


Question 17.
public interface Foo
{
int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
1. final int k = 4;
2. public int k = 4;
3. static int k = 4;
4. abstract int k = 4;
5. volatile int k = 4;
6. protected int k = 4;
  1.    1, 2 and 3
  2.    2, 3 and 4
  3.    3, 4 and 5
  4.    4, 5 and 6
 Discuss Question
Answer: Option A. -> 1, 2 and 3

(1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly public, 

static, and final. Interface constant declarations of public, static, and final are optional in any combination.


Question 18.

Which three are legal array declarations?

  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];


  1.    1, 2, 4
  2.    2, 4, 5
  3.    2, 3, 4
  4.    All are correct.
 Discuss Question
Answer: Option A. -> 1, 2, 4

(1), (2), and (4) are legal array declarations. With an array declaration, you can place the 

brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal 

to split the brackets in a multidimensional array, and place them on both sides of the identifier. 

Although coding this way would only annoy your fellow programmers, for the exam, you need 

to know it's legal.

(3) and (5) are wrong because you can't declare an array with a size. The size is only needed 

when the array is actually instantiated (and the JVM needs to know how much space to allocate 

for the array, based on the type of array and the size).


Question 19.

Which is a reserved word in the Java programming language?


  1.    method
  2.    native
  3.    subclasses
  4.    reference
  5.    array
 Discuss Question
Answer: Option B. -> native

The word "native" is a valid keyword, used to modify a method declaration.

Option A, D and E are not keywords. Option C is wrong because the keyword for subclassing 

in Java is extends, not 'subclasses'.


Question 20.

Which is a valid keyword in java?


  1.    interface
  2.    string
  3.    Float
  4.    unsigned
 Discuss Question
Answer: Option A. -> interface

interface is a valid keyword.

Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.

Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.

Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.


Latest Videos

Latest Test Papers