Java Programming OOPs
Questions 91 to 100
91.
|
Which one
of these is a valid method declaration?
|
||||||||||
92.
|
Given a
class named Book, which one of these is a valid constructor declaration for
the class?
|
||||||||||
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)
}
}
|
||||||||||
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.
|
||||||||||
95.
|
Which one
of the following class definitions is a valid definition of a class that
cannot be instantiated?
|
||||||||||
96.
|
Which one
of the following class definitions is a valid definition of a class that
cannot be extended?
|
||||||||||
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.
|
||||||||||
98.
|
How
restrictive is the default accessibility compared to public, protected, and
private accessibility?
|
||||||||||
99.
|
Which
statement is true about
accessibility of members?
|
||||||||||
100.
|
Which of
the following is true about the
use of modifiers?
|
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.
|
thank you.
ReplyDeletejava tutorial
java tutorial
Thanks for the article
ReplyDeleteVisit me back if u dont mind~ FXFBS
nice blog..
ReplyDeleteMachine Learning Online Training In India
Machine Learning Online Training