Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by whitelisting our website.

Java Programming

Which of the following would compile without error?

Question: Which of the following would compile without error?
[A].

int a = Math.abs(-5);

[B].

int b = Math.abs(5.0);

[C].

int c = Math.abs(5.5F);

[D].

int d = Math.abs(5L);

Answer: Option A

Explanation:

The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.

In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to “int a”.

The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an “int”.

Which of the following would compile without error? Read More »

Java Programming, Java.lang Class

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

Question: You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?
[A].

public

[B].

private

[C].

protected

[D].

default access

Answer: Option D

Explanation:

The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective? Read More »

Declarations And Access Control, Java Programming

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

Question: You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
[A].

public

[B].

private

[C].

protected

[D].

transient

Answer: Option C

Explanation:

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.

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective? Read More »

Declarations And Access Control, Java Programming

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

Question: Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?
[A].

final

[B].

static

[C].

private

[D].

protected

Answer: Option C

Explanation:

The private access modifier limits access to members of the same class.

Option A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers.

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class? Read More »

Declarations And Access Control, Java Programming