Which two cause a compiler error?
1.float[ ] f = new float(3);
2.float f2[ ] = new float[ ];
3.float[ ]f1 = new float[3];
4.float f3[ ] = new float[3];
5.float f5[ ] = {1.0f, 2.0f, 2.0f};
Answer: D
(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3];
(2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3];
(3), (4), and (5) compile without error.
Enter details here
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
Answer: C
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.
Enter details here
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?
Answer: A
Option A is correct - because the class that extends A is just simply overriding method1.
Option B is wrong - because it can't override as there are less access privileges in the subclass method1.
Option C is wrong - because to override it, the return type needs to be an integer. The different return type means that the method is not overriding but the same argument list means that the method is not overloading. Conflict - compile time error.
Option D is wrong - because you can't override a method and make it a class method i.e. using static.
Enter details here
Which of the following is/are legal method declarations?
1.protected abstract void m1();
2.static final void m1(){}
3.synchronized public final void m1() {}
4.private native void m1();
Answer: D
All the given statements are legal declarations.
Enter details here
Which of the following class level (nonlocal) variable declarations will not compile?
Answer: C
Option C will not compile; the synchronized modifier applies only to methods.
Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.
Enter details here