Click here for 20 free practice questions
Declarations and Access Control
Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization.
-
What is length field?
All arrays in Java have a public final field known as length. This field is used to find the number of elements that the array holds. Once an array is created, its size can be obtained by using the length field qualified by the array name along with a dot operator. For example, the given statement finds the size of the array named MyArray.
MyArray.length;
The length field of an array always has a non-negative value. If the length of an array is zero, it is called an empty array.
What is array initializer?
An array initializer is a list of values or expressions used to simultaneously create and initialize the elements of an array. The values contained in an array initializer are separated by commas and placed within curly braces, i.e., between "{" and "}". When an array initializer is used to initialize an array, there is no need to specify the size of the array. The number of elements inside the array initializer determines the size of the array.
For a given class, determine if a default constructor will be created and if so state the prototype of that constructor.
Identify legal return types for any method given the declarations of all related methods in this or parent classes.
Flow control, Assertions, and Exception Handling
Write code using if and switch statements and identify legal argument types for these statements.
-
What is the if-else statement?
- The argument to the if statement must be a boolean expression.
- If the argument evaluates to true, then the statements in the if-block are executed.
- If the argument evaluates to false, then the statements in the else-block are executed.
- If the number of statements after the if clause is more than one, it is necessary to put them inside the curly braces.
The if-else statement is a control flow statement. It is used to execute a statement or group of statements based on some condition.
The general format of the statement is given below:
if(boolean_expression)
{statements;}
else
{statements;}
The else clause is optional.
The following points must be noted about the if statement:
Write code using all forms of loops including labeled and unlabeled, use of break and continue, and state the values taken by loop counter variables during and after loop execution.
Write code that makes proper use of exceptions and exception handling clauses (try, catch, finally) and declares methods and overriding methods that throw exceptions.
Recognize the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination).
Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions.
Identify correct statements about the assertion mechanism.
Garbage Collection
State the behavior that is guaranteed by the garbage collection system.Write code that explicitly makes objects eligible for garbage collection.
Recognize the point in a piece of source code at which an object becomes eligible for garbage collection.
Language Fundamentals
Identify correctly constructed package declarations, import statements, class declarations (of all forms including inner classes) interface declarations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers.Identify all Java programming language keywords. Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants.
- What are keywords?
Every computer language uses some reserved words known as keywords. They are an essential part of the language. In Java, keywords are defined in lower case and cannot be used as identifiers.
The table given below shows the list of keywords used in the Java language.
The keywords 'const' and 'goto' have not yet been implemented in Java. Java has reserved them for use in future expansion of the language.
Note: In addition to the keywords shown in the table, Java reserves the boolean values (true and false) and the reference value (null) for its own use.
| abstract | assert | boolean | break | byte | case | catch |
| char | class | const | continue | default | do | double |
| else | extends | final | finally | float | for | goto |
| if | implements | import | instanceof | int | interface | long |
| native | new | package | private | protected | public | return |
| short | static | srictfp | super | switch | synchronzzed | this |
| throw | throws | transient | try | void | volatile | while |
