Sail E0 Webinar

MCQs

Total Questions : 51 | Page 5 of 6 pages
Question 41.

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


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

(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

<identifier> expected A variable name is expected after a[ ] .


Question 42.
interface Base
{
boolean m1 ();
byte m2(short s);
}


which two code fragments will compile?


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) }}
  1.    1 and 2
  2.    2 and 3
  3.    3 and 4
  4.    1 and 5
 Discuss Question
Answer: Option C. -> 3 and 4

(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 43.

What is the process by which we can control what parts of a program can access the 

members of a class?


  1.    Polymorphism
  2.    Abstraction
  3.    Encapsulation
  4.    Recursion
 Discuss Question
Answer: Option C. -> Encapsulation

None.


Question 44.

Which of the following loops will execute the body of loop even when condition controlling

the loop is initially false?


  1.    do-while
  2.    while
  3.    for
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> do-while

None.


Question 45.

Which of these is used as default for a member of a class if no access specifier is used for it?


  1.    private
  2.    public
  3.    public, within its own package
  4.    protected
 Discuss Question
Answer: Option A. -> private

When we pass an argument by call-by-value a copy of argument is made into the formal parameter 

of the subroutine and changes made on parameters of subroutine have no effect on original argument,

 they remain the same.


Question 46.

Which of these are selection statements in Java?


  1.    if()
  2.    for()
  3.    continue
  4.    break
 Discuss Question
Answer: Option A. -> if()

continue and break are jump statements, and for is an looping statement.


Question 47.
public class Outer
{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }
public static void main(String[] argv)
{
Outer ot = new Outer();
//Line 10
}
}


Which of the following code fragments inserted, will allow to compile?


  1.    new Inner(); //At line 5
  2.    new Inner(); //At line 10
  3.    new ot.Inner(); //At line 10
  4.    new Outer.Inner(); //At line 10
 Discuss Question
Answer: Option A. -> new Inner(); //At line 5

Option A compiles without problem.

Option B gives error - non-static variable cannot be referenced from a 

static context.

Option C package ot does not exist.

Option D gives error - non-static variable cannot be referenced from a 

static context.


Question 48.

You want subclasses in any package to have access to members of a superclass. 

Which is the most restrictive access that accomplishes this objective?


  1.    public
  2.    private
  3.    protected
  4.    transient
 Discuss Question
Answer: Option C. -> protected

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 C - protected. A is also a contender but C is more restrictive, 

B 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 49.
Which of these selection statements test only for equality?
  1.    if
  2.    switch
  3.    if & switch
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> switch

switch statements checks for equality between the controlling variable and its constant cases.


Question 50.

Which of these is used to access member of class before object of that class is created?


  1.    public
  2.    private
  3.    static
  4.    protected
 Discuss Question
Answer: Option C. -> static

None.


Latest Videos

Latest Test Papers