1.10. Answers to sample exam questions

Q1-1.

Given:

class EJava {
    //..code
}

Which of the following options will compile?


  1. package java.oca.associate;
    class Guru {
        EJava eJava = new EJava();
    }

  2. package java.oca;
    import EJava;
    class Guru {
        EJava eJava;
    }

  3. package java.oca.*;
    import java.default.*;
    class Guru {
        EJava eJava;
    }

  4. package java.oca.associate;
    import default.*;
    class Guru {
        default.EJava eJava;
    }
  5. 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 statement. Options c and d try to import classes from nonexistent packages—java.default and default.

Q1-2.

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

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

Q1-3.

Which of the following examples defines a correct Java class structure?


  1. #connect java compiler;
    #connect java virtual machine;
    class EJavaGuru {}

  2. package java compiler;
    import java virtual machine;
    class EJavaGuru {}

  3. import javavirtualmachine.*;
    package javacompiler;
    class EJavaGuru {
        void method1() {}
        int count;
    }

  4. package javacompiler;
    import javavirtualmachine.*;
    class EJavaGuru {
        void method1() {}
        int count;
    }

  5. #package javacompiler;
    $import javavirtualmachine;
    class EJavaGuru {
        void method1() {}
        int count;
    }

  6. 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.

Q1-4.

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 {}

  1. The imported class, java.util.Date, can be accessed only in the class Student.
  2. The imported class, java.util.Date, can be accessed by both the Student and Course classes.
  3. Both of the classes Student and Course are defined in the package com.ejava-guru.
  4. 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.

Q1-5.

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

  1. one:two:three
  2. EJavaGuru:one:two
  3. java:EJavaGuru:one
  4. 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.

Q1-6.

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");
    }
}

  1. public void main (String[] args)
  2. public void main(String args[])
  3. static public void main (String[] array)
  4. public static void main (String args)
  5. 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 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.

Q1-7.

What is the meaning of “write once, run anywhere”? Select the correct options:

  1. Java code can be written by one team member and executed by other team members.
  2. It is for marketing purposes only.
  3. It enables Java programs to be compiled once and can be executed by any JVM without recompilation.
  4. 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.

Q1-8.

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;
}

  1. import mycode.com.ejavaguru.Course;
  2. import com.ejavaguru.Course;
  3. import mycode.com.ejavaguru;
  4. import com.ejavaguru;
  5. import mycode.com.ejavaguru*;
  6. 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.*;

Q1-9.

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?

  1. The class EJavaGuru will print Java.
  2. The class EJavaGuru will print null.
  3. The class EJavaGuru won’t compile.
  4. 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.

Q1-10.

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);
    }
}

  1. The class EJavaGuru will print Java.
  2. The class EJavaGuru will print null.
  3. The class EJavaGuru will not compile.
  4. 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.

Q1-11.

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;
    }
}

  1. You can’t define a method argument as a private variable.
  2. A method argument should be defined with either public or default accessibility.
  3. For overridden methods, method arguments should be defined with protected accessibility.
  4. None of the above.

Answer: a

Explanation: You can’t add an explicit accessibility keyword to the method parameters. If you do, the code won’t compile.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.73