The following sample questions are extracted from Enthuware OCAJF Java Foundations Certification Exam Simulator, which contains more than 250 practice questions with detailed explanations.
CLICK HERE TO PURCHASE
Consider the following method...
public int setVar(int a, int b, float c) {
//valid code not shown
}
Which of the following methods correctly overload the above method ?
Click to see discussion about this Question!
A method is said to be overloaded when there is another method available in the same class with the same name but different the parameter list ( either the number or their order) are different.
Note that method name and parameter type list (parameter names do not matter) together make up a method's signature. Thus, overloaded methods have same method name but different signatures.
Option 2 is not valid because of the line: return this(a, c, b); This is is the syntax of calling a constructor and not a method. It should have been: return this.setVar(a, c, b);
How many string objects are created in the following code fragment?
Sting a, b, c;
a = new String("hello");
b = a;
c = a + b;
Click to see discussion about this Question!
Note: String interning is a complex topic with many nuances. It is not mentioned explicitly in exam objectives but a few candidates have reported seeing such questions in the exam. You should remember the following rules about this topic:
The JVM maintains a pool of all the String objects. Whenever you use a String literal (i.e. a string within double quotes), the JVM checks whether that string already exists in the pool or not. If the string exists, then it uses the same String object instead of creating a new one. This is called interning of Strings.
When you create a string using the new operator, interned strings are not used and a new String object is created.
So, for example,
String s1 = "hello"; //new interned string object containing hello is created
String s2 = "hello"; //no new object is created because the same String already exists in the string pool
String s3 = new String("hello"); //new string object is created in the heap area.
In the above code, only two string objects are created (not three).
What will the following statement print?
int marks = 90;
String exam = "OCJA";
System.out.printf("I scored %d marks in the %s exam!", exam, marks );
Click to see discussion about this Question!
Which digits and in what order will be printed when the following program is run?
public class TestClass
{
public static void main(String args[])
{
int k = 0;
try{
int i = 5/k;
}
catch (ArithmeticException e){
System.out.println("1");
}
catch (RuntimeException e){
System.out.println("2");
return ;
}
catch (Exception e){
System.out.println("3");
}
finally{
System.out.println("4");
}
System.out.println("5");
}
}
Click to see discussion about this Question!
Division by 0 throws an ArithmeticException. This is caught by the first catch clause since it is the first block that can handle arithmetic exceptions. This prints 1. Now, as the exception is already handled, control goes to finally which prints 4 and then the try/catch/finally ends and 5 is printed.
Remember : finally is always executed even if try or catch return; The only exception to this rule is System.exit().
Given:
class Node{
static final int TYPE = 100;
public static void print(){
System.out.println(TYPE); //1
}
}
public class Test{
public static void main(String[] args) {
//INSERT CODE HERE //2
}
}
What may be done to the above code to make it print 100?
Click to see discussion about this Question!
Given:
String str = "hello\r\n" + "world";
System.out.println(str.length);
Click to see discussion about this Question!
1. \r and \n are valid escape sequences for carriage return and new line characters respectively.
2. String class does not have a field named length. It has a method named length. So, str.length will not compile. But str.length() will return 12.
Which of the following can be valid declarations of an integer variable?
Select 2 options:Click to see discussion about this Question!
What can you do to make the following code print a number between 0 and 10?
(Assume appropriate import statements.)
int x = 10;
//insert code here
System.out.println(d);
Click to see discussion about this Question!
java.util.Random class has two constructors:
Random(): Creates a new random number generator.
Random(long seed): Creates a new random number generator using a single long seed.
The Random(long seed) constructor is equivatent to doing Random r = new Random(); r.setSeed(long seed);
Random class has the following two methods that return an int:
int nextInt():
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. All 2^32 possible int values are produced with (approximately) equal probability.
int nextInt(int bound)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
Please go through JavaDoc API description of java.util.Random class.
Which of the following are reserved words in Java?
Select 2 options:Click to see discussion about this Question!
You are writing a class named AccountManager. This class is the starting point of your application and is to be executing from the command line. What should be the name of the file containing this class's source code?
Select 1 best option:Click to see discussion about this Question!
Given:
public class Account {
int id;
public Account(int id){
this.id = id;
}
public static void main(String[] args) {
List<Account> list = new ArrayList<Account>();
list.add(new Account(111));
list.add(new Account(222));
//insert code here
}
}
Which of the following options, when inserted in the above code, will print 111 222 ?
Click to see discussion about this Question!
What is the effect of compiling and running this class ?
public class TestClass
{
public static void main (String args [])
{
int sum = 0;
for (int i = 0, j = 10; sum > 20; ++i, --j) // 1
{
sum = sum+ i + j;
}
System.out.println("Sum = " + sum);
}
}
Click to see discussion about this Question!
What will the following code print?
int x = 1;
int y = 2;
int z = x++;
int a = --y;
int b = z--;
b += ++z;
int answ = x>a?y>b?y:b:x>z?x:z;
System.out.println(answ);
Click to see discussion about this Question!
This is a simple but frustratingly time consuming question. Expect such questions in the exam.
For such questions, it is best to keep track of each variable on the notepad after executing each line of code.
You also need to be clear about how prefix and postfix increment/decrement operators works. The basic principle is that in case of the prefix operator, the variable is updated and then its value is used in the expression, while in case of the postfix operator, the current value of the variable is used in the expression and then the variable is update.
For example,
int z = x++;
Here, the current value of x is 1 (as given in the code). So, 1 will be the resulting value of the expression x++. Next, x is incremented to 2. Therefore, after this statement, z will be 1 but x will be 2.
int a = --y;
Here, the current value of y is 2 (as given in the code). So, y will be decremented to 1 and then its new value i.e. 1 will be the result of the expression --y. Therefore, after this statement, a will be 1 and y will also be 1.
Similarly, after the execution of int b = z--;, b will be 1 but z will be 0.
Expand b += ++z; to b = b + ++z; Thus, b = 1 + (++z). Since z is 0, this will reduce to b = 1 + 1 (applying the prefix rule explained above). Thus, b will be 2 and z will 1.
The final values of the variables are as follows -
x=2 y=1 z=1 a=1 b=2
The expression x>a?y>b?y:b:x>z?x:z; should be grouped as -
x > a ? (y>b ? y : b) : (x>z ? x : z);
It will, therefore, assign 2 to answ.
Which of the following are features of Java?
Select 2 options:Click to see discussion about this Question!
Given:
public class Test
{
public int div(int a, int b) throws Exception {
try{
return a/b;
}catch(ArithmeticException ae){
System.out.println("exception in div");
return 0;
}
}
public static void main(String args[])
{
Test test = new Test();
try{
System.out.println(test.div(5, 0));
}catch(Exception e){
System.out.println("exception in main");
}
}
}
What is the output?
Click to see discussion about this Question!
Division by zero causes an ArithmeticException to be thrown in the div method. This exception is caught by the catch block in div method. The catch block prints "exception in div" and return 0. The method ends here. Observe that the exception is not thrown to the caller of the div method because the exception is caught within the method itself. As far as the caller is concerned, the call to div returns normally without any exception.
Therefore, the print statement in main prints 0. The control does not go to the catch block in main and the program ends.
Given:
public class Test
{
static int a;
int b;
public void incr(){
int c = a++;
b++;
c++;
System.out.println(a+" "+b+" "+c);
}
public static void main(String args[])
{
Test test = new Test();
test.incr();
a++;
test = new Test();
test.incr();
}
}
What will be the output?
Click to see discussion about this Question!
You need to remember the following points to answer this question:
1. static fields belong to the class and not to the instance of a class. Therefore, there is only 1 copy of the field 'a' irrespective of the number of objects of class Test that are created.
2. Every instance of a class gets its own copy of the instance fields. Therefore, every Test instance gets its own copy of the field 'b'.
3. The postfix increment operation increments the value of the variable after the expression is evaluated. Therefore, in the statement int c = a++; first, the existing value of a is assigned to c and then a is incremented.
Based on the above, it is easy to see the following:
1. in the first call to incr(), c is set to 0, then a is incremented to 1, then b is incremented to 1, and then c is incremented to 1.
Therefore, it prints 1 1 1.
2. Next, a is incremented to 2 in the main method.
3. Next, the second call to incr() is invoked on a different instance of Test. Therefore, in this new instance, a is 2 (because a is static) and b is 0.
Now, c gets 2, then a is incremented to 3. then b is incremented to 1, then c is incremented to 3.
Therefore, 3 1 3 is printed.