chapter 1 sample exam questions Flashcards
Java basics
Given: class EJava { //..code } Which of the following options will compile? a package java.oca.associate; class Guru { EJava eJava = new EJava(); } b package java.oca; import EJava; class Guru { EJava eJava; } c package java.oca.*; import java.default.*; class Guru { EJava eJava; } d package java.oca.associate; import default.*; class Guru { default.EJava eJava; } e None of the above
Answer: e Explanation: A class that isn’t defined in a package gets implicitly defined in Java’s default package. But such classes can’t be accessed by classes or interfaces, which are explicitly defined in a package. Option a is incorrect. The EJava class isn’t defined in a package, so it can’t be accessed by the Guru class, which is defined in the java.oca.associate package. Options b, c, and d won’t compile. Option b uses invalid syntax in the import state-ment. Options c and d try to import classes from nonexistent packages—java.default and default.
The following numbered list of Java class components is not in any particular order. Select the correct order of their occurrence in a Java class (choose all that apply): 1 comments 2 import statement 3 package statement 4 methods 5 class declaration 6 variables a 1, 3, 2, 5, 6, 4 b 3, 1, 2, 5, 4, 6 c 3, 2, 1, 4, 5, 6 d 3, 2, 1, 5, 6, 4
Answer: a, b, d Explanation: The comments can appear anywhere in a class. They can appear before and after package and import statements. They can appear before or after a class, method, or variable declaration. The first statement (if present) in a class should be a package statement. It can’t be placed after an import statement or a declaration of a class. The import statement should follow a package statement and be followed by a class declaration. The class declaration follows the import statements, if present. It’s followed by the declaration of the methods and variables. Answer c is incorrect. None of the variables or methods can be defined before the definition of a class or interface.
Which of the following examples defines a correct Java class structure? a #connect java compiler; #connect java virtual machine; class EJavaGuru {} b package java compiler; import java virtual machine; class EJavaGuru {} c import javavirtualmachine.*; package javacompiler; class EJavaGuru { void method1() {} int count; } d package javacompiler; import javavirtualmachine.*; class EJavaGuru { void method1() {} int count; } e #package javacompiler; $import javavirtualmachine; class EJavaGuru { void method1() {} int count; } f package javacompiler; import javavirtualmachine; Class EJavaGuru { void method1() {} int count; }
Answer: d
Explanation: Option a is incorrect because #connect isn’t a statement in Java. # is
used to add comments in UNIX.
Option b is incorrect because a package name (Java compiler) can’t contain
spaces. Also, java virtual machine isn’t a valid package name to be imported in a
class. The package name to be imported can’t contain spaces.
Option c is incorrect because a package statement (if present) must be placed
before an import statement.
Option e is incorrect. #package and $import aren’t valid statements or directives
in Java.
Option f is incorrect. Java is case-sensitive, so the word class is not the same as the
word Class. The correct keyword to define a class is class.
Given the following contents of the Java source code file MyClass.java, select the correct options: // contents of MyClass.java package com.ejavaguru; import java.util.Date; class Student {} class Course {} a The imported class, java.util.Date, can be accessed only in the class Student. b The imported class, java.util.Date, can be accessed by both the Student and Course classes. c Both of the classes Student and Course are defined in the package com.ejava-guru. d Only the class Student is defined in the package com.ejavaguru. The class Course is defined in the default Java package.
Answer: b, c
Explanation: You can define multiple classes, interfaces, and enums in a Java source
code file.
Option a is incorrect. The import statement applies to all the classes, interfaces,
and enums defined within the same Java source code file.
Option d is incorrect. If a package statement is defined in the source code file, all
the classes, interfaces, and enums defined within it will exist in the same Java package.
Given the following definition of the class EJavaGuru,
class EJavaGuru {
public static void main(String[] args) {
System.out.println(args[1]+”:”+ args[2]+”:”+ args[3]);
}
}
what is the output of the previous class, if it is executed using the following command?
java EJavaGuru one two three four
a one:two:three
b EJavaGuru:one:two
c java:EJavaGuru:one
d two:three:four
Answer: d
Explanation: The command-line arguments passed to the main method of a class do
not contain the word Java and the name of the class.
Because the position of an array is zero-based, the method argument is assigned
the following values:
args[0] -> one
args[1] -> two
args[2] -> three
args[3] -> four
The class prints two:three:four.
Which of the following options, when inserted at //INSERT CODE HERE, will print out EJavaGuru? public class EJavaGuru { // INSERT CODE HERE { System.out.println("EJavaGuru"); } }
a public void main (String[] args) b public void main(String args[]) c static public void main (String[] array) d public static void main (String args) e static public main (String args[])
Answer: c
Explanation: Option a is incorrect. This option defines a valid method but not a valid
main method. The main method should be defined as a static method, which is miss-ing from the method declaration in option a.
Option b is incorrect. This option is similar to the method defined in option a,
with one difference. In this option, the square brackets are placed after the name of
the method argument. The main method accepts an array as a method argument, and
to define an array, the square brackets can be placed after either the data type or the
method argument name.
Option c is correct. Extra spaces in a class are ignored by the Java compiler.
Option d is incorrect. The main method accepts an array of String as a method
argument. The method in this option accepts a single String object.
Option e is incorrect. It isn’t a valid method definition and doesn’t specify the
return type of the method. This line of code will not compile.
What is the meaning of “write once, run anywhere”? Select the correct options:
a Java code can be written by one team member and executed by other team
members.
b It is for marketing purposes only.
c It enables Java programs to be compiled once and can be executed by any JVM
without recompilation.
d Old Java code doesn’t need recompilation when newer versions of JVMs are
released.
Answer: c
Explanation: Platform independence, or “write once, run anywhere,” enables Java
code to be compiled once and run on any system with a JVM. It isn’t for marketing
purposes only.
A class Course is defined in a package com.ejavaguru. Given that the physical location of the corresponding class file is /mycode/com/ejavaguru/Course.class and execution takes place within the mycode directory, which of the following lines
of code, when inserted at // INSERT CODE HERE, will import the Course class into the class MyCourse? // INSERT CODE HERE class MyCourse { Course c; } a import mycode.com.ejavaguru.Course; b import com.ejavaguru.Course; c import mycode.com.ejavaguru; d import com.ejavaguru; e import mycode.com.ejavaguru*; f import com.ejavaguru*;
Answer: b
Explanation: Option a is incorrect. The base directory, mycode, in which package
com.ejavaguru is defined, must not be included in the import statement.
Options c and e are incorrect. The class’s physical location isn’t specified in the
import statement.
Options d and f are incorrect. ejavaguru is a package. To import a package and its
members, the package name should be followed by ., as follows:
import com.ejavaguru.;
Examine the following code: class Course { String courseName; } class EJavaGuru { public static void main(String args[]) { Course c = new Course(); c.courseName = "Java"; System.out.println(c.courseName); } } Which of the following statements will be true if the variable courseName is defined as a private variable? a The class EJavaGuru will print Java. b The class EJavaGuru will print null. c The class EJavaGuru won’t compile. d The class EJavaGuru will throw an exception at runtime.
Answer: c
Explanation: If the variable courseName is defined as a private member, it won’t be accessible from the class EJavaGuru. An attempt to do so will cause it to fail at compile time. Because the code won’t compile, it can’t execute.
Given the following definition of the class Course, package com.ejavaguru.courses; class Course { public String courseName; } what’s the output of the following code? package com.ejavaguru; import com.ejavaguru.courses.Course; class EJavaGuru { public static void main(String args[]) { Course c = new Course(); c.courseName = "Java"; System.out.println(c.courseName); } } a The class EJavaGuru will print Java. b The class EJavaGuru will print null. c The class EJavaGuru will not compile. d The class EJavaGuru will throw an exception at runtime.
Answer: c Explanation: The class will fail to compile because a nonpublic class can’t be accessed outside a package in which it’s defined. The class Course therefore can’t be accessed from within the class EJavaGuru, even if it’s explicitly imported into it. If the class itself isn’t accessible, there’s no point in accessing a public member of a class.
Given the following code, select the correct options: package com.ejavaguru.courses; class Course { public String courseName; public void setCourseName(private String name) { courseName = name; } } a You can’t define a method argument as a private variable. b A method argument should be defined with either public or default accessibility. c For overridden methods, method arguments should be defined with protected accessibility. d None of the above.
Answer: a
Explanation: You can’t add an explicit accessibility keyword to the method parame-ters. If you do, the code won’t compile.