Level 120 Level 122
Level 121

[Quiz] Encapsulation


21 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
True.
In Java, Exceptions are objects. True or false?
False. The class will fail to compile.
True of False: It is valid to have code that can be executed only after a return statement.
False.
True or False: The enhanced for loop can be used to initialize an array and modify its elements.
Define Constructor.
Constructors have the same name as the class, and they create and return an instance of the class in which they are defined. They don't specify a return type—not even void.
When does the JVM create a default constuctor for a class?
Default constructors are defined by Java, but only if the developer doesn't define any constructor in a class.
What is an initializer block?
It is a block of code defined within a class, not as a part of a method. It executes for every object that's created for a class.
True. The initializer block will execute prior to the constructor.
True or False: If you define both an initializer and a constructor for a class, both of these will execute.
True or False: An initializer block can not create local variables.
False: It can access and assign values to instance and static variables. It can call methods and define loops, conditional statements, and try-catch-finally blocks.
How does a constructor call another constructor of the same class?
A constructor can call another overloaded constructor by using the keyword this. A constructor can't invoke a constructor by using its class's name. If present, a call to another constructor must be the first statement in a constructor.
Define a well-encapsulated class.
A well-encapsulated object doesn't expose the internal parts of an object outside it. It defines a set of well-defined interfaces (methods), which enables the users of the class to interact with it. By exposing ob…
True. The primitive doesn't change, regardless of whether the method reassigns the primitive to another variable or modifies it.
True or False: When you pass a primitive variable to a method, its value remains the same after the execution of the method.
Will an overloaded method that declares a float match this method call: overloadedMethod(3.14)?
No. The default type for a fractional literal is double. So methods only having only Double, double, and Object parameters will match.
Look through the list of overloaded methods to find the one that matches the signature.
When given a question like "Which overloaded method will be invoked with overloadedMethod(objectX)" what answer should you look for?
Protected. This access level is useful for subclasses overriding methods.
Which access modifier allows access only to subclasses or other classes within the same package? Give one example where this access is desirable.
public float getAgeInMonths() {}
False. Including the two method declarations in the same class will result in a compilation error since they both have the same signature. A method signature consists of the method name, type, and order of parameters.
demo.max(45.0, 0.056f);
False. The default type for decimal numbers is double. The method call will not compile due to a loss in precision error. The method signature has two floats for parameters and can not accommodate a double, float input.
What access modifiers are allowed in the declaration of a top-level class?
public and the [DEFAULT, no modifier]. private would be useless since the class wouldn't be visible at all. protected could not be applied since there would be a "chicken-egg" problem with limiting access to classes that extend it.
method(Object foo) {}
The overloaded method having the Object parameter will get called. VarArg method parameter matching is only attempted by the compiler if no other matches can first be found. The Object match is found first.
What is the JVM rules for matching overloaded methods
Exact first, then widening, then boxing/unboxing, then varargs last.
class
All Java methods must be contained in a ...
Attributes
Data is generally stored in columns and rows similar to a spreadsheet as _______.