OCJP Practice Papers - Java Basics

OCJP Practice Papers - Java Basics : Covers all the basic questions

Which of the following method signatures is a valid declaration of an entry point in aJava application?
A. public void main(String[] args)
B. public static void main()
C. private static void start(String[] mydata)
D. public static final void main(String[] mydata)

The following class diagram demonstrates the relationship between Gold and Silver,which extend the Metal class. Assume the attributes are all declared public. Whichstatement about the diagram is not true? Fig:2 A.The diagram demonstrates platform independence in Java.
B. The diagram demonstrates object-oriented design in Java.
C. The Gold and Silver classes inherit weight and color attributes from the Metalclass.
D. Gold does not inherit the luster attribute.

What is the proper filename extension for a Java bytecode compiled file?
A. .java
B. .bytecode
C. .class
D. .dll

Given that a Date class exists in both the java.util and java.sql packages, what is theresult of compiling the following class?

import java.util.*;
import java.sql.*;
public class BirthdayManager {
private Date rob = new Date();
private java.util.Date sharon = new java.util.Date();
}

A. The code does not compile because of lines 1 and 2.
B. The code does not compile because of line 4.
C. The code does not compile because of line 5.
D. The code compiles without issue.

Which of the following is not a facet of traditional object-oriented programminglanguages?
A. Objects are grouped as procedures, separate from the data they act on.
B. An object can take many forms via casting.
C. An object can hold data, referred to as attributes.
D. An object can perform actions, via methods.

Which variables have a scope limited to a method?
A. Interface variables
B. Class variables
C. Instance variables
D. Local variables

Which package is imported into every Java class by default?
A. java.util
B. java.lang
C. system.lang
D. java.system

Which of the following is not a valid code comment in Java?
A. // Add 5 to the result
B. /*** TODO: Fix bug 12312 ***/
C. # Add configuration value
D. /* Read file from system ****/

Which statement about a valid .java file is true?
A. It can only contain one class declaration.
B. It can contain one public class declaration and one public interface definition.
C. It must define at least one public class.
D. It may define at most one public class.

Given the following application, fill in the missing values in the table starting from thetop and going downward. Java Basic Que 2
A. 2, 0, 1
B. 2, 2, 1
C. 1, 0, 1
D. 0, 2, 1

Which statement about import statements is true?
A. The class will not compile if it contains unused import statements.
B. Unused import statements can be removed from the class without causing a classto become unable to be compiled.
C. The class will not compile if a duplicate import statement is present.
D. If a class contains an import statement for a class used in the program that cannotbe found, it can still compile.

What is the result of compiling and executing the following class?

1: public class ParkRanger {
2: int birds = 10;
3: public static void main(String[] data) {
4: int trees = 5;
5: System.out.print(trees+birds);
6: }
7: }

A. It does not compile.
B. It compiles but throws an exception at runtime.
C. It compiles and outputs 5.
D. It compiles and outputs 15.

Which statements about Java are true? I. The java command can execute .java and .class files. II. Java is not object oriented. III. The javac command compiles directly into native machine code.
A. I only
B. III only
C. II and III
D. None are true.

Which of the following lines of code is not allowed as the first line of a Java class file?
A. import widget.*;
B. // Widget Manager
C. package sprockets;
D. int facilityNumber;

Which one of the following statements is true about using packages to organize yourcode in Java?
A. Every class is required to include a package declaration.
B. To create a new package, you need to add a package.init file to the directory.
C. Packages allow you to limit access to classes, methods, or data from classes outsidethe package.
D. It is not possible to restrict access to objects and methods within a package.

Given that the current directory is /user/home, with an application Java file in/user/home/Manager.java that uses the default package, which are the correctcommands to compile and run the application in Java?
A. javac Manager java Manager
B. javac Manager.java java Manager
C. javac Manager java Manager.class
D. javac Manager.java

java Manager.class Structuring a Java class such that only methods within the class can access itsinstance variables is referred to as _______.
A. platform independence
B. object orientation
C. inheritance
D. encapsulation

What is the output of the following code snippet?

String tree = "pine";
int count = 0;
if (tree.equals("pine")) {
int height = 55;
count = count + 1;
}System.out.print(height + count);

A. 1
B. 55
C. 56
D. It does not compile.

Which of the following is true of a Java bytecode file?
A. It can be run on any computer with a compatible JVM.
B. It can only be executed on the same type of computer that it was created on.
C. It can be easily read and modified in a standard text editor.
D. It requires the corresponding .java that created it to execute.

What is the correct character for terminating a statement in Java?
A. A colon (:)
B. An end-of-line character
C. A tab character
D. A semicolon (;)

What is the result of compiling and executing the following class?

1: public class Tolls {
2: private static int yesterday = 1;
3: int tomorrow = 10;
4: public static void main(String[] args) {
5: Tolls tolls = new Tolls();
6: int today=20, tomorrow = 40;
7: System.out.print(today + tolls.tomorrow + tolls.yesterday);
8: }
9: }

A. The code does not compile due to line 6.
B. The code does not compile due to line 7.
C. 31
D. 61

Given the following class definition, which is the only line that does not contain acompilation error?

1: public ThisClassDoesNotCompile {
2: double int count;
3: void errors() {}
4: static void private limit; }

A. Line 1
B. Line 2
C. Line 3
D. Line 4

Which of the following features allows a Java class to be run on a wide variety ofcomputers and devices?
A. Encapsulation
B. Object oriented
C. Inheritance
D. Platform independence

Which of the following is not a property of a JVM?
A. It prevents Java bytecode from being easily decoded/decompiled.
B. It supports platform independence.
C. It manages memory for the application.
D. It translates Java instructions to machine instructions.

Which of the following variables are always in scope for the entire program?
A. Package variables
B. Class variables
C. Instance variables
D. Local variables

Given the following wildcard import statements, which class would be included in theimport? import television.actor.*; import movie.director.*;
A. television.actor.recurring.Marie
B. movie.directors.John
C. television.actor.Package
D. movie.NewRelease

Which is the correct order of statements for a Java class file?
A. import statements, package statement, class declaration
B. package statement, class declaration, import statement
C. class declaration, import statements, package declaration
D. package statement, import statements, class declaration

Given the following class definition, what is the maximum number of importstatements that can be discarded and still have the code compile? For this question,assume that the Blackhole class is defined only in the stars package.

package planetarium;
import java.lang.*;
import stars.*;
import java.lang.Object;
import stars.Blackhole;
public class Observer {
public void find(Blackhole blackhole) {}
}

A. Zero
B. One
C. Two
D. Three

Given the following class definition, which command will cause the application tooutput the message White-tailed?

package forest;
public class Deer {
public static void main(String... deerParams) {
System.out.print(theInput[2]);
}
}

A. java forest.Deer deer 5 "White-tailed deer"
B. java forest.Deer "White-tailed deer" deer 3
C. java forest.Deer Red deer White-tailed deer
D. java forest.Deer My "deer White-tailed"

Which of the following is a true statement?
A. The java command compiles a .java file into a .class file.
B. The javac command compiles a .java file into a .class file.
C. The java command compiles a .class file into a .java file.
D. The javac command compiles a .class file into a .java file.

Which of the following statements about Java is true?
A. Java is a procedural programming language.
B. Java allows method overloading.
C. Java allows operator overloading.
D. Java allows direct access to objects in memory.

Given the following code, what values inserted in order into the blank lines, allow thecode to compile?

_______agent;
public _______Banker {
private static _______getMaxWithdrawal() {
	return 10;
}
}

A. import, class, null
B. import, interface, void
C. package, int, int
D. package, class, long

What is the output of the following application?

public class Airplane {
static int start = 2;
final int end;
public Airplane(int x) {
x = 4;
end = x;
}
public void fly(int distance) {
System.out.print(end-start+" ");
System.out.print(distance);
}
public static void main(String... start) {
new Airplane(10).fly(5);
}
}

A. 2 5
B. 8 5
C. 6 5
D. The code does not compile.

What is one of the most important reasons that Java supports extending classes viainheritance?
A. Inheritance requires that a class that extends another class be in the same package.
B. The program must spend extra time/resources at runtime jumping through multiple layers of inheritance to determine precise methods and variables.
C. Method signature changes in parent classes may break subclasses that use overloaded methods.
D. Developers minimize duplicate code in new classes by sharing code in a common

parent class. Which of the following is a valid code comment in Java?
A. //////// Walk my dog
B. #! Go team!
C. / Process fails at runtime /
D. None of the above

Which of the following method signatures is not a valid declaration of an entry pointin a Java application?
A. public static void main(String... arguments)
B. public static void main(String arguments)
C. public static final void main(String[] arguments)
D. public static void main(String[] arguments)

Given the file Magnet.java below, which of the marked lines can you independently insert the line public String color; into and still have the code compile?

// line a1
public class Magnet {
// line a2
public void attach() {
// line a3
}
// line a4
}

A. a1 and a3
B. a2 and a4
C. a2, a3, and a4
D. a1, a2, a3, and a4

What is required to define a valid Java class file?
A. A class declaration
B. A package statement
C. At least one import statement
D. The public modifier

What is the proper filename extension for a Java source file?
A. .jav
B. .class
C. .source
D. .java

Given that a Math class exists in both the java.lang and pocket.complex packages,what is the result of compiling the following class?

1: package pocket;
2: import pocket.complex.*;
3: import java.util.*;
4: public class Calculator {
5: public static void main(String[] args) {
6: System.out.print(Math.floor(5));
7: }
8: }

A. The code does not compile because of line 2.
B. The code does not compile because of line 3.
C. The code does not compile because of line 6.
D. The code compiles without issue.

Given a class that uses the following import statements, which class would not beautomatically accessible within the class without using its full package name?

import dog.*;
import dog.puppy.*;

A. dog.puppy.female.KC
B. dog.puppy.Georgette
C. dog.Webby
D. java.lang.Object

._______is the technique of structuring programming data as a unit consisting ofattributes, with actions defined on the unit.
A. Encapsulation
B. Object orientation
C. Platform independence
D. Polymorphism

Given the following class definition, what is the maximum number of importstatements that can be discarded and still have the code compile? For this question, assume that the Broccoli class is in the food.vegetables package, and the Apple classis the food.fruit package.

package food;
import food.vegetables.*;
import food.fruit.*;
import java.util.Date;
public class Grocery {
Apple a; Broccoli b; Date c;
}

A. 0
B. 1
C. 2
D. 3

Given the following application, what is the expected output?

public class Keyboard {
private boolean numLock = true;
static boolean capLock = false;
public static void main(String... shortcuts) {
System.out.print(numLock+" "+capLock);
}
}

A. true false
B. false false
C. It does not compile.
D. It compiles but throws an exception at runtime.

What is the result of compiling and executing the following class?

public class RollerSkates {
static int wheels = 1;
int tracks = 5;
public static void main(String[] arguments) {
RollerSkates s = new RollerSkates();
int feet=4, tracks = 15;
System.out.print(feet + tracks + s.wheels);
}
}

A. The code does not compile.
B. 5
C. 10
D. 20

What is the result of compiling and executing the following class?

package sports;
public class Bicycle {
String color = "red";
private void printColor(String color) {
color = "purple";
System.out.print(color);
}
public static void main(String[] rider) {
new Bicycle().printColor("blue");
}
}

A. red
B. purple
C. blue
D. It does not compile.

Which statements about calling the compilation command javac and the execution command java are true? I. java may use a period . to separate packages. II. javac takes a .java file and returns a .class file. III. java may use a slash (/) to separate packages.
A. I only
B. II only
C. I and II
D. I, II, and III

What is the result of compiling and executing the following application?

package forecast;
public class Weather {
private static boolean heatWave = true;
public static void main() {
boolean heatWave = false;
System.out.print(heatWave);
}
}

A. true
B. false
C. It does not compile.
D. It compiles but throws an error at runtime.

Given the following class diagram, which Java implementation most closely matchesthis structure?
A.

public class Book {
public int numOfPages;

B.
public class Book {
public String getRating() {return null;} }

C.
public class Book {
public int numberOfPages;
public String getRating() {return null;} }

D.
public class Book {
void numberOfPages; 
}
Which statement about the JVM is true?
A. The JVM schedules garbage collection on a predictable schedule.
B. The JVM ensures that the application will always terminate.
C. The JVM requires a properly defined entry point method to execute theapplication.
D. A Java compiled code can be run on any computer.

All OCJP Java Certification Tutorials

  1. Oracle (OCJP) Java Certification Exam
  2. Java Building Blocks Java 8 Certification
  3. Operators And Statements
  4. Java Core API
  5. Java Class Design
  6. Java Exception
  7. Java Try-Catch Block
  8. Exceptional Handling in Java
  9. Common Exception in Java

OCJP Practice Test, Certification Path, Tips/Tricks

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency