Java Programming Questions and Answers Set 10

Java Programming OOPs

Questions 91 to 100



91.
Which one of these is a valid method declaration?
(a)
void method1
(b)
void method2()
(c)
void method3(void)
(d)
method4()
(e)
methods(void).
92.
Given a class named Book, which one of these is a valid constructor declaration for the class?
(a)
Book(Book b) { }
(b)
Book Book() { }
(c)
private final Book() { }
(d)
void Book() { }
(e)
abstract Book() { }.
93.
What will be the result of attempting to compile the following program?
       public class MyClass {
       long var;
       public void MyClass(long param) { var = param; }    //(1)
       public static void main(String[] args) {
              MyClass a,b;
              a = new MyClass();                  //(2)
              b = new MyClass(5);                //(3)
              }
       }
(a)
A compilation error will occur at (1), since constructors cannot specify a return value
(b)
A compilation error will occur at (2), since the class does not have a default constructor
(c)
A compilation error will occur at (3), since the class does not have a constructor which takes one argument of type int
(d)
The program will compile correctly
(e)
The program will compile and execute correctly.
94.
Given the following class, which of these is valid way of referring to the class from outside of the package net.basemaster?
package net.basemaster;
public class Base {
// . . .
}
Select the correct answer.
(a)
By simply referring to the class as Base
(b)
By simply referring to the class as basemaster.Base
(c)
By simply referring to the class as net.basemaster.Base
(d)
By simply referring to the class as net.Base
(e)
By importing with net.* and referring to the class as basemaster.Base.
95.
Which one of the following class definitions is a valid definition of a class that cannot be instantiated?
(a)
class Ghost
       {
              abstract void haunt();   
       }
(b)
abstract class Ghost
       {
              void haunt();  
       }
(c)
abstract class Ghost
       {
              void haunt() { };     
       }
(d)
abstract Ghost
       {
              abstract void haunt();   
       }
(e)
static class Ghost
       {
              abstract haunt();
       }
96.
Which one of the following class definitions is a valid definition of a class that cannot be extended?
(a)
class Link { }
(b)
abstract class Link { }
(c)
native class Link { }
(d)
static class Link { }
(e)
final class Link { }.
97.
Given the following definition of a class, which fields are accessible from outside the package com.corporation.project?
package com.corporation.project;
public class MyClass
{
       int i;
       public int j;
       protected int k;
       private int l;
}
Select the correct answer.
(a)
Field i is accessible in all classes in other packages
(b)
Field j is accessible in all classes in other packages
(c)
Field k is accessible in all classes in other packages
(d)
Field l is accessible in all classes in other packages
(e)
Field l is accessible in subclasses only in other packages.
98.
How restrictive is the default accessibility compared to public, protected, and private accessibility?
(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.
99.
Which statement is true about accessibility of members?
(a)
Private members are always accessible from within the same package
(b)
Private members can only be accessed by code from within the class of the member
(c)
A member with default accessibility can be accessed by any subclass of the class in which it is defined
(d)
Private members cannot be accessed at all
(e)
Package/default accessibility for a member can be declared using the keyword default.
100.
Which of the following is true about the use of modifiers?
(a)
If no accessibility modifier (public, protected, and private) is specified for a member declaration, the member is only accessible for classes in the package of its class and subclasses of its class anywhere
(b)
You cannot specify accessibility of local variables. They are only accessible within the block in which they are declared
(c)
Subclasses of a class must reside in the same package as the class they extend
(d)
Local variables can be declared static
(e)
None of the above.

Answers


91.
Answer : (b)
Reason:  Only (b) is a valid method declaration. Methods must specify a return  type or are declared void. This makes (d) and (e) invalid. Methods must specify a list of zero or more comma-separated parameters delimited by (). The keyword void is not a valid type for a parameter. This makes (a) and (c) invalid.
92.
Answer : (a)
Reason:  A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
93.
Answer : (c)
Reason:  A compilation error will occur at (3), since the class does not have a constructor accepting single argument of type int. The declaration at (1) declares a method, not a constructor, since it is declared as void. The  method happens to have the same name as the class, but that is irrelevant. The class has an implicit default constructor since the class contains no constructor declarations. This constructor is invoked to create a MyClass object at (2).
94.
Answer : (c)
Reason:  A class or interface name can be referred to by using either its fully qualified name or its simple name. Using the fully qualified name will always work, but in order to use the simple name it has to be imported.
95.
Answer : (c)
Reason:  A class is uninstantiable if the class is declared abstract. The declaration of an abstract method cannot provide an implementation. The declaration of a non-abstract method must provide an implementation. If any method in a class is declared abstract, then the class must be declared abstract. Definition (d) is not valid since it omits the class keyword.
96.
Answer : (e)
Reason:  A class can be extended unless it is declared final. For classes, final means it cannot be extended, while for methods, final means it cannot be overridden in a subclass. A nested static class, (d), can be extended. The keyword native can only be used for methods, not for classes and fields.
97.
Answer : (b)
Reason:  Outside the package, member j is accessible to any class, whereas member k is only accessible to subclasses of MyClass.
Field i has package accessibility and is only accessible by classes inside the package. Field j has public accessibility and is accessible from anywhere. Field k has protected accessibility and is accessible from any class inside the package and from subclasses anywhere. Field l has private accessibility and is only accessible within its own class.
98.
Answer : (c)
Reason:  The default accessibility for members is more restrictive than protected accessibility, but less restrictive than private. Members with default accessibility are only accessible within the class itself and from classes in the same package. Protected members are in addition accessible from subclasses anywhere. Members with private accessibility are only accessible within the class itself.
99.
Answer : (b)
Reason:  A private member is only accessible by code from within the class of the member. If no accessibility modifier has been specified, a member has default accessibility, also known as package accessibility. The keyword default is not an accessibility modifier, and its only use is as a label in a switch statement. Members with package accessibility are only accessible from classes in the same package. Subclasses outside the package cannot access members with default accessibility.
100.
Answer : (b)
Reason:  You cannot specify accessibility of local variables. They are accessible only within the block in which they are declared.
              Objects themselves do not have any accessibility, only references to objects do. If no accessibility modifier (public, protected, or private) is given in the member declaration of a class, the member is only accessible to classes in the same package. A class does not have access to members with default accessibility declared in a superclass, unless both classes are in the same package. Inheritance has no consequence with respect to accessing members with default accessibility in the same package. Local variables cannot be declared static or given an accessibility modifier.


<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


3 comments :