Chapter 1: Java Basics -sample exam questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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 a You can't define a method argument as a private variable. Explanation: You can't add an explicit accessibility keyword to the method parameters. If you do, the code won't compile.

What are the valid components of a Java source file (choose all that apply): a. package statement b. import statements c. methods d. variables e. Java compiler f. Java Runtime Environment

Answer: a, b, c, d a package statement b import statements c methods d variables Explanation: The Java compiler and Java Runtime Environment aren't components of a Java source file.

Select the correct options: a) You can start the execution of a Java application through the main method. b) The Java compiler calls and executes the main method. c) The Java Virtual Machine calls and executes the main method. d) A class calls and executes the main method.

Answer: a, c a) You can start the execution of a Java application through the main method. c) The Java Virtual Machine calls and executes the main method. Explanation: The Java Virtual Machine calls and executes the main method.

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 b) import com.ejavaguru.Course; Explanation: Option (a) is incorrect. The path of the imported class used in an import statement isn't related to the class's physical location. It reflects the package and subpackage that a class is in. 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.*;

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.ejavaguru. d) Only the class Student is defined in the package com.ejavaguru. The class Course is defined in the default Java package.

Answer: b and c 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.ejavaguru. 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 of the classes, interfaces, and enums defined within it will exist in the same Java package.

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 won't compile. d The class EJavaGuru will throw an exception at runtime.

Answer: c c The class EJavaGuru will not compile. Explanation: The class will fail to compile because a non-public class cannot be accessed outside a package in which it is defined. The class Course therefore can't be accessed from within the class EJavaGuru, even if it is explicitly imported into it. If the class itself isn't accessible, there's no point in accessing a public member of a class.

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 class EJavaGuru will print Java. b class EJavaGuru will print null. c class EJavaGuru won't compile. d class EJavaGuru will throw an exception at runtime.

Answer: c c. class EJavaGuru won't compile. 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.

Why won't the following code compile? import certification.ExamQuestion; package university; class AnnualExam { ExamQuestion eq; }

The code won't compile because an import statement can't be placed before a package statement

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

a. 1, 3, 2, 5, 6, 4 b. 3, 1, 2, 5, 4, 6 d. 3, 2, 1, 5, 6, 4 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.

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[])

c) static public void main (String[] array) 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 missing 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.

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

d two:three:four 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 examples define the 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; }

d) package javacompiler; import javavirtualmachine.*; class EJavaGuru { void method1() {} int count; } Explanation: Answer (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) cannot contain spaces. Also, java virtual machine isn't a valid package name to be imported in a class. The package name to be imported cannot contain spaces. Option (c) is incorrect because a package statement should 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.


Set pelajaran terkait

Cell Biology: Describe how the lipid bilayer is produced and assembled and identify and discuss the factors that impact fluidity of the lipid bilayer

View Set

International Business Ch. 1 - Globalization

View Set

Chapter 20: Acute Spinal Cord Injury

View Set

WGU - D089 - Principles of Economics

View Set

Chapter 3 - The Costs of Production and Profit Maximization

View Set