OCAJP 7 Mock Exam Questions (Java Basics)

SHARE & COMMENT :

Q 1 : Which of the following are keywords in Java programming language

  • a) public
  • b) static
  • c) void
  • d) main
  • e) scope

Q 2 : Consider the code

private static void printTotal(int i, int j) {
int sum = i + j;
System.out.println("This program performs addition " +
"using two numbers supplied.");
System.out.println("The two numbers and the sum: " +
i + " + " + j + " = " + sum);
}

In the above code which of the following define local variables:

  • a) i, j only.
  • b) sum only.
  • c) i, j and sum.
  • d) There are no local variables.

Q 3 : Which of the following are correct regarding Java variables

  • a) The default value for boolean variable is: true.
  • b) A literal and a variable are the same.
  • c) This is a valid variable declaration: int _52 = 52;
  • d) This is a valid instance variable declaration: int i, j = 10;

Q 4 : Consider the code:

// This program prints text and an integer number.
public class YearPrinter {
public static void main(String [] args) {
int year= 2013;
System.out.println("This year is: " + year);
}
}

Which of the following are correct of the above code:

  • a) “year” is a numeric variable.
  • b) ‘System.out.println(“This year is: ” + year);’ is a statement.
  • c) “YearPrinter” is the name of a class.
  • d) “main” is the name of a method.
  • e) “//” is used to write a comment within a class.

Q 5 : Which of the following code snippets correctly show statement “blocks”

a.

public int calculate(int a, int b) {
int c = a + b;
System.out.println("a+b = " + c);
return c;
}

b.

String a = "Java";
String b = "programming";
String c = "Java programming";
if (c.equals(a + b)) {
System.out.println("Java programming"); // prints Java programming
}

c.

{
Object object = null;
}

d.

Integer i = null;
try {
i = new Integer("56");
}
catch(NumberFormatException nfe) {
nfe.printStackTrace();
}

Q 6 : To create a Java application which of the following are required

  • a) A source code file.
  • b) Java programming language compiler.
  • c) A text editor or an IDE.
  • d) The Java application launcer.

Q 7 : Consider the code

public class YearPrinter {
public static void main(String [] args) {
new YearPrinter().printIt();
}
private void printIt() {
int year= 2013;
System.out.println("This year is: " + year);
}

}

Which one of the following is correct after the above is compiled and run:

  • a) There is a compile time error.
  • b) The program prints “The year is: 2013″.
  • c) There is a runtime error.

Q 8 : Consider the code

package mypackage;

public class ClassA {
public static void main(String [] args) {
new ClassA().doThis();
}
public void doThis() {
System.out.println("Running the ClassA class.");
}
}

Assume that the ClassA.java is in the current directory. And, all the Java commands are run from the current directory. Which of the following are correct:

  • a) The command “javac -d . ClassA.java” at command prompt creates a directory called mypackage, and ClassA.class file within that directory.
  • b) The above class can be run at the command prompt using the command “java mypackage.ClassA”.
  • c) The above class can be run at the command prompt using the command “java ClassA”.
  • d) The above class can be run at the command prompt using the command “java ClassA.class”.

Q 9 : The package and import statements must be specified before a class definition within a class file

True or false:

Q 10 : Consider the following two Java classes

ProgramA.java:

import java.util.ArrayList;
public class ProgramA {

public static void main (String [] rrr) {
ArrayList<String> list1 = new ArrayList<String>();
list1.add("element 1");
System.out.println("An element is added to the array list.");
}
}

ProgramB.java:

public class ProgramB {

public static void main (String [] args) {
java.util.ArrayList<String> list = new java.util.ArrayList<String>();
list.add("element 1");
System.out.println("An element is added to the array list.");
}
}

Which of the following are correct of the above two classes:

  • a) Both ProgramA and ProgramB compile, run and print “An element is added to the arraylist .”.
  • b) Only ProgramA compiles, runs and prints “An element is added to the arraylist .”.
  • c) Only ProgramB compiles, runs and print “An element is added to the arraylist .”.
  • d) Both ProgramA and ProgramB fail to compile and run.

OCAJP Mock Exam Answers

The following are the answers for the above questions. these questions are created based on the real questions on the exam. If you find any incorrect or clarifications on the answers, please write it in the comments section.

If you are interested in receiving 300 mock questions for OCAJP 7 from JavaBeat, please call +91 9740134466

1.  a, b and c are correct.
d and e are incorrect.
d: main is the name of a method. This is the method a Java program is launched from.
e: scope is not a keyword.

2. c is correct.
a, b and d are incorrect.
c: The variables defined within a method are local variables. This includes the method parameters i and j in the above code.

3. c and d are correct.
a and b are incorrect.
a: The default value for boolean variable is “false”.
b: Literals and variables are not the same. A literal is a value assigned to a variable. There are two types of literals; String and primitive type. An example of a String literal is “This is a String literal”. And, an example of a primitive literal is 52.
d: The variable i is initialized to 0, and j is initialized to 10.

4. a, b, c, d and e are all correct.
5. a, b, c and d are correct.
A block is a group of statements of code defined within curly braces (“{ }”).
6. a, b, c and d are correct.
All are required. “Javac” is the Java compiler, and “java” is the application launcher required to compile the source code and run the application, respectively. A text editor may be used to create a Java application (for example, MyApp) in a source code file with the name like “MyApp.java”.
7. b is correct.
a and c are incorrect.

8. a and b are correct.
c and d are incorrect.

a: “-d” option used with “javac” specifies the destination of the class file. “.” is used to specify the current directory. The class file is created as  mypackage/ClassA.class in the current directory.
b: The program runs and prints “Running the ClassA class.”.
c: Throws runtime exception. The class name is wrong.
d: Prints Error: Could not find or load main class ClassA.class. The extension “.class” is not to be specified when a Java class is run.

9. True.
The package statement must be the first statement in a class file, followed by import statements. Both package and import statements are optional. There can be only one package statement, and zero or more import statements. There can be comments specified before a package statement.

10. a is correct.
b, c and d are incorrect.
a: ArrayList class is defined in the java.util package of Java programming language API. In ProgramA, the import statement makes the ArrayList class available within the class. In ProgramB the ArrayList class is used with a fully qualified name, java.util.ArrayList.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Petras Petkus says:

    I find a couple of exam questions a bit ambiguos:

    > Q4 e): “//” is used to write a comment within a class.

    Actually the comment in question’s example is written outside class definition. Therefore it may not be considered “within”. Besides, one should remember that source file (i.e. compilation unit) may contain several top level type definitions. In that case it become completely unclear within which class the comment is.

    > Q6 : To create a Java application which of the following are required
    > The given correct answer is: all options.

    But the question asks what is _required_ to create, i.e. it asks what elements are absolutely mandatory. I’m not sure it is obvious that application creation at bare minimum requires an editor and launcher since:
    * (a) points that I already have a complete source file;
    * Java compiler, not launcher, creates an application to be launched.

    The question does not indicate, what elements are mandatory for editing or launching an application, it asks for “create” and therefore the given answer should be: a (a source file) and b (compiler).

  2. Erik Karkheck says:

    Q3: d. The variable i isn’t initialized so it doesn’t equal anything and is unusable until it is initialized.

  3. Q7′s answer (I think is a.) Because without creating an object, you cannot access the method…
    YearPrinter yp = new YearPrinter(),
    yp.printIt();
    I checked and got the following error:
    Error: cannot find symbol.

  4. There are some real questions from exam?

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better