
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive set of questions and answers covering key concepts for the java oca certification exam. it delves into various aspects of java programming, including data types, exception handling, access modifiers, and the java.time package. The questions test understanding of core java principles and best practices, making it a valuable resource for students and professionals preparing for the exam.
Typology: Exams
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















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). 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: 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. 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. 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. 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. A try statement must always have a ............. associated with it. - catch, finally or both 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 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 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. What are the primitive data types? - Java has only the following primitive data types: boolean, byte, short, char, int, long, float and double.
determine if a given application should be thrown explicitly by the programmer or not. 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. True or false? "An abstract class can be extended by an abstract or a concrete class." - True True or false? "An abstract class cannot implement an interface." - False Any class, whether abstract or concrete, can implement any interface. True or false? "A concrete class can be extended by an abstract or a concrete class." - True True or false? "An interface can be extended by another interface." - True 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? "An interface can be extended by a concrete class." - False
True or false? "The modulus operator % can only be used with integer operands."
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: 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 main method of a program can declare that it throws checked exceptions. - False Any method can do that! 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: 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() ) 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. 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. 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 initializes the instance members of the class. - False True or False: The "default" constructor calls the no-args constructor of the super class. - True True or False: The "default" constructor initializes instance as well as class fields of the class. - False 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: private keyword can never be applied to a class. - False
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() Is this a valid declaration in a class? abstract int absMethod(int param) throws Exception; - Yes 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? float native getVariance() throws Exception; - No return type should always be on the immediate left of method name. 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? 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 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: A class cannot override the super class's constructor. - True Because constructors are not inherited. True or False: It is possible for two classes to be the superclass of each other. - False 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 { } 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: 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: subclass of a final class can be abstract. - False final class cannot be subclassed. 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: An interface may extend an interface. - True Unlike a class, an interface can extend from multiple interfaces. 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: A class can implement an interface and extend a class. - True True or False: A class can extend an interface and can implement a class. - False True or False: An interface can only be implemented and cannot be extended. - False It can be extended by another interface. 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. 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. 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: You cannot specify visibility of local variables. - True They are always only accessible within the block in which they are declared. 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: 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 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 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 character literal can be used as a value for a case label. - True boolean, long, float and double cannot be used. True or False: A 'long' cannot be used as a switch variable. - True boolean, long, float and double cannot be used. True or False: An empty switch block is a valid construct. - True True or False: A switch block must have a default label. - False True or False: If present, the default label must be the last of all the labels. - False Any order is valid. 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. 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. 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: 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: 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: 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: 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.
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 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: 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: All classes must explicitly define a constructor. - False A default no args one will be provided if not defined any.