Java Basic

Quiz Guidelines


Multiple Choice Question


Compared to public, protected and private accessibility, default accessibility is...

Select 1 option

A. Less restrictive than public
B. More restrictive than public, but less restrictive than protected.
C. More restrictive than protected, but less restrictive than private.
D. More restrictive than private.
E. Less restrictive than protected from within a package, and more restrictive than protected from outside a package.

Multiple Choice Question


How can you declare 'i' so that it is not visible outside the package test?

package test;
public class Test{
XXX int i;
/* irrelevant code */

Select 2 options

A. private
B. public
C. protected
D. No access modifier
E. friend

Multiple Choice Question


Consider the following two classes (in the same package but defined in different source files):

public class Square {
      double side = 0;
      double area;
         public Square(double length){ this.side = length; }
        public double getSide() { return side; }
        public void setSide(double side) { this.side = side; }
       double getArea() { return area; }
public class TestClass {
      public static void main(String[] args) throws Exception {
      Square sq = new Square(10.0);
      sq.area = sq.getSide()*sq.getSide();
       }   }

You are assigned the task of refactoring the Square class to make it better in terms of encapsulation. What changes will you make to this class?

Select 2 options

A. Make setSide() method private. 
B. Make getArea() method private. 
C. Make side and area fields private. 
D. Make the side field private and remove the area field. 
E. Change getArea method to:
         public double getArea(){ return side*side; }
F. Add a setArea() method.

Multiple Choice Question


Which of the given options can be successfully inserted at line 1....

//line 1
   public class A{

Select 3 options

A. import java.lang.*;
B. package p.util;
C. public class MyClass{ } 
D. abstract class MyClass{ }

Multiple Choice Question


When a class whose members should be accessible only to members of that class is coded such a way that its members are accessible to other classes as well, this is called ...

Select 1 option

A. strong coupling
B. weak coupling
C. strong typing
D. weak encapsulation
E. weak polymorphism
F. high cohesion
G. low cohesion