Modulo 1 - Chapter 1 - Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Chapter 1

Question 1
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


Chapter 1

Question 2
The following numbered list of Java class components is not in any particular
order. Select the acceptable order of their occurrence in any 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
Chapter 1

Question 3
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;
}
Chapter 1

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

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.
Chapter 1

Question 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 EJavaGuru, 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
Chapter 1

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

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

Question 7
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.
Chapter 1

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

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*;
Chapter 1

Question 9
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.
Chapter 1

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

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.
Chapter 1

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

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.
Chapter 1

Question 12
Which of the following statement is true?

a. Java applications never run out of memory as GC manages the memory.


b. An object is eligible for GC when there is no reference to that object.
c. The purpose of GC is to delete objects that is of no use at the moment.
d. When you request GC to run, it will start immediately.
e. Object Class has a final() method. *GC – Garbage Collector
Chapter 1

Question 13
A Java class will always have _________ .
a. A main method.
b. Variable(s).
c. At least one method.
d. A Constructor.
e. All of the above.
Chapter 1

Question 14
What will be the output of this program?

a. 10
b. No output will be produced.
c. Compilation fails due to error at line 1.
d. Compilation fails due to error at line 8.
e. Compilation fails due to multiple errors.
Chapter 1

Question 15
How many objects are eligible for GC at the execution of //line 14?

a. 0
b. 1
c. 2
d. 3
e. Compilation fails.
Chapter 1

Question 16
What will be the output of this program code?

a. true false
b. false false
c. An Exception is thrown.
d. Compilation fails due to error at line 7.
e. Compilation fails due to error at line 8.
Chapter 1

Question 17
Given the following classes, what is the maximum number of imports that can be
removed and have the code still compile?
package aquarium; public class Water { }
package aquarium;
import java.lang.*;
import java.lang.System;
import aquarium.Water;
import aquarium.*;
public class Tank {
public void print(Water water) {
System.out.println(water); } }

A. 0
B. 1
C. 2
D. 3
E. 4
F. Does not compile.
Chapter 1

Question 18
Given the following class, which of the following is true? (Choose all that apply)

A. If String result = "done"; is inserted on line 2, the code will compile.


B. If String result = "done"; is inserted on line 4, the code will compile.
C. If String result = "done"; is inserted on line 6, the code will compile.
D. If String result = "done"; is inserted on line 9, the code will compile.
E. None of the above changes will make the code compile.
Chapter 1

Question 19
Which of the following legally fill in the blank so you can run the main() method
from the command line? (Choose all that apply)

public static void main( )


A. String[] _names
B. String[] 123
C. String abc[]
D. String _Names[]
E. String... $n
F. String names
G. None of the above.
Chapter 1

Question 20
Which of the following are true? (Choose all that apply)
public class Bunny {
public static void main(String[] args) {
Bunny bun = new Bunny();
}
}

A. Bunny is a class.
B. bun is a class.
C. main is a class.
D. Bunny is a reference to an object.
E. bun is a reference to an object.
F. main is a reference to an object.
G. None of the above.
Chapter 1

Question 21
Given the following classes, which of the following can independently replace INSERT
IMPORTS HERE to make the code compile? (Choose all that apply)

package aquarium;
public class Tank { }

package aquarium.jellies;
public class Jelly { }

package visitor;
INSERT IMPORTS HERE
public class AquariumVisitor {
public void admire(Jelly jelly) { } }

A. import aquarium.*;
B. import aquarium.*.Jelly;
C. import aquarium.jellies.Jelly;
D. import aquarium.jellies.*;
E. import aquarium.jellies.Jelly.*;
F. None of these can make the code compile.

You might also like