Java Developer OCA Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

An instance member ...

1 can be a variable, a constant or a method. 2 belongs to an instance of the class. An instance member belongs to a single instance, not the class as a whole. An instance member is a member variable or a member method that belongs to a specific object instance. All non-static members are instance members.

True or False: A constructor must initialize all the member variables of a class.

False All non-final instance variables get default values if not explicitly initialized. Constructors need not initialize *all* the member variables of the class. A non-final member(i.e. an instance) variable will be assigned a default value if not explicitly initialized.

True or False: An if statement can have either an 'if' clause or an 'else' clause.

False An if-statement must always have an 'if' clause. 'else' is optional.

True or False: continue without a label, can occur only in a switch, while, do, or for statement.

False It cannot occur in a switch.

True or False: Conversion from int to float needs a cast.

False It does not need a cast because a float can hold any int value. Note that the opposite is not true.

Tell me about the keyword implements.

The keyword implements is used when a class inherits method prototypes from an interface. The keyword extends is used when an interface inherits from another interface, or a class inherits from another class.

True or False: A class can implement an interface and extend a class.

True

True or False: A functional interface must have exactly one abstract method and may have other default or static methods.

True

True or False: An empty switch block is a valid construct.

True

True or False: Conversion from byte to short does not need a cast.

True

True or False: Conversion from char to long does not need a cast.

True

True or False: If a and b are of type boolean, the expression (a = b) can be used as the condition expression of an if statement.

True

True or False: If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).

True

True or False: In Java, Strings are immutable. A direct implication of this is that you cannot change a String object, once it is created.

True

True or False: The "default" constructor calls the no-args constructor of the super class.

True

True or False: The "default" constructor is provided by the compiler only if the class does not define any constructor.

True

True or False: The "default" constructor takes no arguments.

True

True or False: The extends keyword is used to specify inheritance.

True

True or False: You can either call super(<appropriate list of arguments>) or this(<appropriate list of arguments>) but not both from a constructor.

True

True or False: break without a label, can occur only in a switch, while, do, or for statement.

True

True or False: subclass of an abstract class can be declared abstract.

True

True or False: synchronized keyword can never be applied to a class.

True

True or false? "A concrete class can be extended by an abstract or a concrete class."

True

True or false? "An abstract class can be extended by an abstract or a concrete class."

True

What class of objects can be declared by the throws clause?

Exception Error RuntimeException You can declare anything that is a Throwable or a subclass of Throwable, in the throws clause.

True or False: A constructor can access the non-static members of a class.

True A constructor is non-static, and so it can access directly both the static and non-static members of the class.

What is the correct declaration for an abstract method 'add' in a class that is accessible to any class, takes no arguments and returns nothing?

abstract public void add() throws Exception;

A try statement must always have a ............. associated with it.

catch, finally or both

Tell me about the final keyword.

final keyword when applied to a class means the class cannot be subclassed, when applied to a method means the method cannot be overridden (it can be overloaded though) and when applied to a variable means that the variable is a constant.

Which class should you use to represent just a date without any timezone information?

java.time.LocalDate Java 8 introduces a new package java.time to deal with dates. The old classes such as java.util.Date are not recommended anymore.

Compared to public, protected, and private accessibilities, default accessibility is....

more restrictive than protected, but less restrictive than private. Members with default accessibility are only accessible within the class itself and from other classes in the same package. protected members are in addition accessible from subclasses in any other package as well. Members with private accessibility are only accessible within the class itself.

What is the correct order of restrictiveness for access modifiers? List them. (First one should be least restrictive)

public protected package (i.e. no modifier) private That's right, protected is less restrictive than package.

True or False: Local variable always have default accessibility

False A local variable (aka automatic variable) means a variable declared in a method. They don't have any accessibility. They are accessible only from the block they are declared in. Remember, they are not initialized automatically. You have to initialize them explicitly.

True or False: A class cannot override the super class's constructor.

True Because constructors are not inherited.

Tell me something about scope.

"class level" means static fields and they can be accessed from anywhere (i.e. static as well as non-static methods) in the class (and from outside the class depending on their accessibility). "instance level" means the instance fields and they can be accessed only from instance methods in the class.

Is it possible to create arrays of length zero?

Yes, you can create arrays of any type with length zero. Example: When a Java program is run without any program arguments, the String[] args argument to main() gets an array of length Zero.

What can be the type of a catch argument?

Any class that is a Throwable. The catch argument type declares the type of exception that the handler can handle and must be the name of a class that extends Throwable or Throwable itself.

Where, in a constructor, can you place a call to a super class's constructor ?

As the first statement in the constructor.

True or False: A constructor can declare a return value.

False

True or False: A two dimensional array is like a rectangular matrix where number of rows and number of columns may be different but each row or each column have the same number of elements.

False

True or False: A two dimensional array is like a square matrix where number of rows and number of columns are same and each row or each column have the same number of elements.

False

True or False: In Java, Strings are immutable. A direct implication of this is that you can change a String object only by the means of its methods.

False

True or False: break can never occur without a label.

False

True or False: continue can never occur with a label.

False

True or False: super(<appropriate list of arguments>) can only be called in the first line of the constructor but this(<appropriate list of arguments>) can be called from anywhere.

False

True or False: All classes must explicitly define a constructor.

False A default no args one will be provided if not defined any.

True or False: In the new Date-Time API of Java 8, LocalDateTime includes time zone information but LocalDate does not.

False None of LocalDate, LocalDateTime, or LocalTime store zone information. java.time.ZonedDateTime does. ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times. For example, the value "2nd October 2007 at 13:45.30.123456789 +02:00 in the Europe/Paris time-zone" can be stored in a ZonedDateTime. ZonedDateTime is not listed in official exam objectives.

True or False: In a two dimensional array the number of rows and columns must be specified at the time it is declared.

False Size of the dimensions is required to be specified only at the time of instantiation and not at the time of declaration. For example,int[][] ia; //this is a valid declaration.int[][] ia = new int[2][3];//This is a valid declaration and a valid instantiationFurther, only the size of the first dimension is required to be specified at the time of instantiation for an array of more than one dimension. Sizes of the other dimensions may be left out.int[][] iaa=new int[3][];int[][][] iaaa = new int[3][][]; //Both are valid. This is allowed because a multi dimensional array in Java is just an array of arrays. They do not have to be symmetric, that is, each sub array is an independent array and so they do not have to be of the same size. So, in the above example, iaa[0] can be initialized to new int[5], and ia[1] to new int[10], while ia[2] can be left null.

True or False: In Java, Strings are immutable. A direct implication of this is you cannot compare String objects.

False String class implements Comparable interface.

True or False: In Java, Strings are immutable. A direct implication of this is you cannot extend String class.

False That's because it is final, not because it is immutable. You can have a final class whose objects are mutable.

True or False: In the new Date-Time API of Java 8, to create a LocalDate or a LocalDateTime object, you can use one of their several constructors.

False These classes do not have any public constructors. You need to use their static factory methods to get their instances.

True or False: An overriding method must have a same parameter list and the same return type as that of the overridden method.

False This would have been true prior to Java 1.5. But from Java 1.5, an overriding method is allowed to change the return type to any subclass of the original return type, also known as covariant return type. This does not apply to primitives, in which case, the return type of the overriding method must match exactly to the return type of the overridden method.

True or False: Only expressions which evaluate to a boolean value can be used as the condition in an if statement.

False Unlike C/C++ where you can use integers as conditions, in java, only booleans are allowed.

True or False: The statement : if (false) ; else ; is illegal.

False if-clause and the else-clause can have empty statements. Empty statement ( i.e. just a semi-colon ) is a valid statement.

True or False: If neither super( ) or this( ) is declared as the first statement of the body of a constructor, then this( ) will implicitly be inserted as the first statement.

False super() is added and not this()

True or False: A super( <appropriate list of arguments> ) or this( <appropriate list of arguments> ) call must always be provided explicitly as the first statement in the body of the constructor.

False super(); is automatically added if the sub class constructor doesn't call any of the super class's constructors.

Under what situations does a class get a default constructor?

If the class does not define any constructors explicitly. In this case, the compiler will add a no args constructor for this class.

True or False: Conversion from byte, char or short to int, long or float does not need a cast.

True Because int, long or float are bigger that byte char or short.

True or False: In Java, Strings are immutable. A direct implication of this is you cannot call methods like "1234".replace('1', '9'); and expect to change the original String.

True Calling such methods do not change this object. They create a new String object.

True or False: Conversion from short to char needs a cast.

True The reverse is also true. Because their ranges are not compatible.

True or False: In the new Date-Time API of Java 8, most of the actual date related classes in the Date-Time API such as LocalDate, LocalTime, and LocalDateTime are immutable.

True These classes do not have any setters. Once created you cannot change their contents. Even their constructors are private.

True or False: The new Date-Time API of Java 8 uses the calendar system defined in ISO-8601 as the default calendar.

True This calendar is based on the Gregorian calendar system and is used globally as the defacto standard for representing date and time. The core classes in the Date-Time API have names such as LocalDateTime, ZonedDateTime, and OffsetDateTime. All of these use the ISO calendar system. If you want to use an alternative calendar system, such as Hijrah or Thai Buddhist, the java.time.chrono package allows you to use one of the predefined calendar systems. Or you can create your own.

True or False: A constructor can be declared private.

True This feature is used for implementing Singleton Classes.

True or False: A multi dimensional array is basically an array of arrays.

True Unlike some other languages, multi dimensional arrays in Java are not like matrices. They are just arrays of arrays. For example, if you have a two dimensional array then each element of this array is a one dimensional array. Each such array element is independent and therefore can be of different lengths (but not of different type).

True or False: The condition expression in an if statement can contain method calls.

True Yes, as long as the method returns a boolean value.

Which operators will always evaluate all the operands?

| and % All mathematical operators evaluate all the operands. || and && are also known as short circuit operators since they do not evaluate the rest of the expression if the value of the expression can be determined by just evaluating part of the expression for example ( true || (bool = false)) will not assign false to bool because the value of the expression can be told just by seeing the first part i.e. true. But ( true | (bool = false)) will assign false to bool.

Which logical operators are known as "short circuiting logical operators"?

|| and && are called short circuiting operators because if, while evaluating a logical expression, at any stage, the value of the whole expression can be determined without evaluating the rest of the expression, then the remaining sub-expressions are not evaluated.

A Java programmer is developing a desktop application. Which of the following exceptions would be appropriate for him to throw explicitly from his code: NullPointerException, ClassCastException, ArrayIndexOutOfBoundsException, Exception, or NoClassDefFoundError?

Exception Notes: 1. NoClassDefFoundError is thrown by the JVM when it attempts to load a class and is unable to find the class file. Note that it extends java.lang.Error and Errors are always thrown by the JVM. A programmer should never throw an Error explicitly. 2. RuntimeExceptions are usually thrown implicitly. A programmer should not throw RuntimeExceptions explicitly. java.lang.Exception and its subclasses (except RuntimeException) should be used by the programmer to reflect known exceptional situations, while RuntimeExceptions are used to reflect unforseen or unrecoverable exceptional situations. 3. There is no hard and fast rule that says RuntimeExceptions (such as the ones mentioned in this questions) must not be thrown explicitly. It is ok to throw these exceptions explicitly in certain situations. For example, framework/library classes such as Struts, Spring, and Hibernate, and standard JDK classes throw these exceptions explicitly. But for the purpose of the exam, it is a good way to determine if a given application should be thrown explicitly by the programmer or not.

True or False: A class can extend an interface and can implement a class.

False

True or False: A functional interface has exactly one method and it may or may not be abstract.

False

True or False: A functional interface has exactly one method and it must be abstract.

False

True or False: A functional interface must have exactly one static method and may have other default or abstract methods.

False

True or False: A switch block must have a default label.

False

True or False: An overriding method can declare that it throws a wider spectrum of checked exceptions than the method it is overriding.

False

True or False: It is possible for two classes to be the superclass of each other.

False

True or False: Local variables can be declared as private.

False

True or False: Local variables can only be declared as public.

False

True or False: The "default" constructor initializes instance as well as class fields of the class.

False

True or False: The "default" constructor initializes the instance members of the class.

False

True or False: To define a default constructor, you must use the default keyword.

False

True or False: subclass of a non-abstract class cannot be declared abstract.

False

True or false? "An interface can be extended by a concrete class."

False

True or false? Both - LocalDate and LocalTime extend LocalDateTime, which extends java.util.Date.

False

True or false? LocalDate, LocalTime, and LocalDateTime extend Date.

False

True or false? LocalDate, LocalTime, and LocalDateTime implement TemporalAccessor and extend java.util.Date.

False

True or false? && can have integer as well as boolean operands.

False !, && and || operate only on booleans.

True or false? "An interface can be extended by an abstract class."

False A class "implements" an interface. It does not "extend" an interface.

True or False: A default constructor is used to return a default value.

False A constructor does not return any value at all. It is meant to initialize the state of an object.

True or False: An interface may extend a class and may implement an interface.

False An interface cannot implement another interface. It can extend another interface but not a class.

True or False: The parameter list of an overriding method must be a subset of the parameter list of the method that it is overriding.

False An overriding method (the method that is trying to override the base class's method) must have the same parameters.

True or false? "An abstract class cannot implement an interface."

False Any class, whether abstract or concrete, can implement any interface.

True or False: The main method of a program can declare that it throws checked exceptions.

False Any method can do that!

True or False: If present, the default label must be the last of all the labels.

False Any order is valid.

True or False: If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.

False Any remaining code of the method will not be executed. Further, any uncaught exception will cause the JVM to kill the thread.

True or False: finally blocks are executed if and only if an exception gets thrown while inside the corresponding try block.

False Finally is ALWAYS executed. (Only exception is System.exit() )

True or False: An interface can implement multiple interfaces.

False Interface cannot "implement" another interfaces. It can extend multiple interfaces. The following is a valid declaration : interface I1 extends I2, I3, I4 { }

True or False: final keyword can never be applied to a class.

False It can be applied to class, variable and methods.

True or False: An interface can only be implemented and cannot be extended.

False It can be extended by another interface.

True or false? "The modulus operator % can only be used with integer operands."

False It can be used on floating points operands also. For example, 5.5 % 3 = 2.5

True or False: synchronized keyword may be applied to a non-primitive variable.

False It can only be applied to a method or a block.

True or False: An overriding method must declare that it throws the same exception classes as the method it overrides.

False It can throw any subset of the exceptions thrown by overridden class.

True or False: The "default" constructor is provided by the compiler if the class does not define a 'no-args' constructor

False It is not provided even if the class declares any other with-args constructor.

True or False: The "default" constructor is provided by the compiler only if the class and any of its super classes does not define any constructor.

False It is provided by the compiler if the class does not define any constructor. It is immaterial if the super class provides a constructor or not.

True or False: By default (i.e. no modifier) the member is only accessible to classes in the same package and subclasses of the class.

False No, the member will be accessible only within the package.

True or False: A class implementing an interface must define all the methods of that interface.

False Not if the class is defined abstract. Further, Java 8 allows an interface to have default and static methods, which need not be implemented by a non-abstract class that says it implements that interface.

True or False: Subclasses must define all the abstract methods that the superclass defines.

False Not if the subclass is also defined abstract!

True or False: A subclass can override any method in a non-final superclass.

False Only the methods that are not declared to be final can be overridden. Further, private methods are not inherited so they cannot be overridden either.

True or False: The "default" constructor is always public.

False The access type of a default constructor is same as the access type of the class. Thus, if a class is public, the default constructor will be public.

True or False: Using a break in a while loop causes the loop to break the current iteration and start the next iteration of the loop.

False The break statement is to break out of any loop completely. So the current iteration and any other remaining iterations of the loop will not execute. Control is transferred to the first statement after the loop.

True or False: A class, in which all the members are declared private, cannot be declared public.

False There is no such rule.

True or False: subclass of a final class can be abstract.

False final class cannot be subclassed.

True or False: private keyword can never be applied to a class.

False private, protected and public can be applied to a nested class. Although not too important for the exam, you should still know the following terminology: A top level class is a class that is not a nested class. A nested class is any class whose declaration occurs within the body of another class or interface.

True or false? ~ can have integer as well as boolean operands.

False ~ operates only on integral types

Tell me something about fields in an interface.

Fields in an interface are implicitly public, static and final. Although you can put these words in the interface definition, it is not a good practice to do so.

What is java.util.function.Predicate?

It is an interface that has only one abstract method (among other non-abstract methods) with the signature public boolean test(T t).

What are the primitive data types?

Java has only the following primitive data types: boolean, byte, short, char, int, long, float and double.

Are private members inherited?

No Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared. Constructors and static initializers are not members and therefore are not inherited.

Is this a valid declaration in a class? abstract native int absMethod(int param) throws Exception;

No native method cannot be abstract.

Is this a valid declaration in a class? abstract private int absMethod(int param) throws Exception;

No private method cannot be abstract. A private method is not inherited so how can a subclass implement it?

Is this a valid declaration in a class? float native getVariance() throws Exception;

No return type should always be on the immediate left of method name.

Does the String class have a reverse method?

No. The String class has no reverse( ) method but StringBuffer (and StringBuilder) do have this method.

Tell me more about how exceptions are handled.

Normal execution will not resume if an exception is uncaught by a method. The exception will propagate up the method invocation stack until some method handles it. If no one handles it then the exception will be handled by the JVM and the JVM will terminate that thread. An overriding method only needs to declare that it can throw a subset of the exceptions the overridden method can throw. Having no throws clause in the overriding method is OK.

How many times can the keyword "package" appear in a Java source file?

Once. There can be at most one package statement in a Java source file and it must be the first statement in the file.

What does the zeroth element of the string array passed to the standard main method contain?

The first argument of the argument list, if present. Note that if no argument is passed to the program, the args parameter is NOT null but a non-null array of Strings of length zero.

List three classes that can be thrown using a throw statement?

Throwable Exception RuntimeException You can only throw a Throwable using a throws clause. Exception and Error are two main subclasses of Throwable.

True or false? "An interface can be extended by another interface."

True

True or false? LocalDate, LocalTime, and LocalDateTime implement TemporalAccessor.

True

True or false? The arithmetic operators *, / and % have the same level of precedence.

True

True or False: Using a continue in a while loop causes the loop to break the current iteration and start the next iteration of the loop.

True A continue causes the next iteration of the loop to start without executing the remaining statements in the loop. The updation section (if it is a for loop) and the condition is also checked before the next iteration of the loop is started.

True or False: A method with no access modifier defined in a class can be overridden by a method marked protected (assuming that it is not final) in the sub class.

True An Overriding method is allowed to make the overridden method more accessible, and since protected is more accessible than default (package), this is allowed. Note that protected access will allow access to the subclass even if the subclass is in a different package but package access will not.

True or False: A final variable can be hidden in a subclass.

True If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.

True or False: The overriding method may opt not to declare any throws clause even if the original method has a throws clause.

True No exception (i.e. an empty set of exceptions) is a valid subset of the set of exceptions thrown by the original method so an overriding method can choose to not have any throws clause.

True or False: A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.

True Note that it cannot throw the instances of any superclasses of the exception.

True or False: Private methods cannot be overridden in subclasses.

True Only methods that are inherited can be overridden and private methods are not inherited.

True or False: You cannot specify visibility of local variables.

True They are always only accessible within the block in which they are declared.

True or False: An interface may extend an interface.

True Unlike a class, an interface can extend from multiple interfaces.

True or False: A 'long' cannot be used as a switch variable.

True boolean, long, float and double cannot be used.

True or False: A character literal can be used as a value for a case label.

True boolean, long, float and double cannot be used.

True or false? & can have integral as well as boolean operands.

True unlike &&, & will not "short circuit" the expression if used on boolean parameters.

What happens when you call System.exit(0) in a try block followed by a finally block?

When you call System.exit(...); The JVM exits so there is no way to execute the finally block.

Is this a valid declaration in a class? abstract int absMethod(int param) throws Exception;

Yes

Identify the valid members of Boolean class: parseBoolean(String ), valueOf(boolean ), parseBoolean(boolean ), FALSE, and/or Boolean(Boolean )?

parseBoolean(String ) valueOf(boolean ) FALSE Note: TRUE and FALSE are valid static members of Boolean class.

How can you declare a method someMethod() such that an instance of the class is not needed to access it and all the members of the same package have access to it?

public static void someMethod() static void someMethod() protected static void someMethod()


Set pelajaran terkait

Interpersonal Communication Midterm

View Set

Unit 1: The Primary Mortgage Market and Institutional Funding Sources

View Set

Live Virtual Machine Lab 2-2: Module 02 Information Gathering & Sharing

View Set

The Uses and Varieties of English

View Set

Orion Accounting Chapter 11 and 13

View Set