You are on page 1of 9

1

Which statement is true about declaring members in a concrete class?


All methods and constructors must contain bodies.

All fields must be initialized explicitly.

Only instance fields and methods are supported.

Only static fields and methods are supported.

Explanation:
All methods and constructors must contain bodies in a concrete class. Only interfaces and abstract
classes support methods without bodies. Constructors cannot be declared without bodies, even in
abstract classes.

All fields do not need to be initialized explicitly. By default, static and instance fields are automatically
initialized to their default values.

Both instance and static fields and methods are supported by concrete classes.

Given:

public class VarScope {


int var;
public static void main (String[] args) {
int var = 10;
VarScope scope = new VarScope();
scope.var = var + 2;
scope.adjustVar(scope.var + 2);
System.out.println("var = " + var);
}
private void adjustVar(int var) {
var += 2;
}
}

What is the result?


var = 10

var = 12

var = 14

var = 16

Explanation:
The result will be the output var = 10. The output is based on the local variable named var in the main
method. The variable var in this scope is set to 10 and not modified until it is printed.

The result will not be the output var = 12 because the local variable in the main method will be printed.
This result would be the output if the instance variable var were printed because the class variable is set
to the local variable incremented by two.

The result will not be the output var = 14 because the local variable in the main method will be printed.
This result would be the output if the local variable var in the adjustVar method were printed before it
is incremented by two.

The result will not be the output var = 16 because the local variable in the main method will be printed.
This result would be the output if the local variable var in the adjustVar method were printed after it is
incremented by two.

Given:
int i2 = i1 + 4;
System.out.println("i2 = " + i2);

Which line of code declares variable(s) so that the code fragment will compile?
int i1;

int i1, i2;

int i1 = 0;

int i1 = 0, i2 = 0;

Explanation:
The following line of code declares variable(s) so that the code fragment will compile:

int i1 = 0;

This code declares the local variable i1 and initializes its value to 0. By default, local variables are not
initialized automatically like instance and class variables.

The code line int i1; will not declare variable(s) so that the code fragment will compile. This code line
declares the i1 variable, but does not set i1 to a value. The code line int i2 = i1 + 4; will not
compile if the i1 variable is not initialized.

The code line int i1, i2; will not declare variable(s) so that the code fragment will compile. This code
line declares the i1 and i2 variable, but does not set either variable to a value. The code line int i2 =
i1 + 4; will not compile if the i1 variable is not initialized and the i2 variable is already declared.

The code line int i1 = 0, i2 = 0; will not declare variable(s) so that the code fragment will
compile. This code line initializes the i1 and i2 variables. The code line int i2 = i1 + 4; will not
compile if the i2 variable is already declared.
4

Given:

public class CommandLineClass {


public static void main(String[] args) {
System.out.print("Argument: " + args[2]);
}
}

and

javac CommandLineClass.java
java CommandLineClass arg1,arg2 arg3

What is the result?


arg1

arg2

arg3

arg1,arg2

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

Explanation:
The result is the following output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

The java command executes compiled Java files and sends arguments as a space-delimited list. The
expression args[2] retrieves the third argument. An ArrayIndexOutOfBoundsException is thrown
because a third argument is not provided in the command.

The result would not be the output arg1 or arg2 because these are substrings of the first argument
arg1,arg2. Command-line arguments are space-delimited only, not comma-delimited.

The result would not be the output arg3 because the expression args[2] retrieves the third argument,
not the second argument.

Given:

package java7;

public class MyBasicClass {}

Which statement is true about packages in MyBasicClass?


MyBasicClass can access members using simple names in the java7 package without an
implicit import statement.
MyBasicClass can access all members using simple names in the same application without an
implicit import statement.
MyBasicClass can only access members using simple names in the java.lang package with an
explicit import statement.
MyBasicClass can only access members using simple names in the same application with an
explicit import statement.

Explanation:
MyBasicClass can access members using simple names in the java7 package without requiring an
implicit import statement. By default, the compiler will automatically import the current package, the
java.lang package, and the package with no name.

MyBasicClass cannot access all members using simple names in the same application without an
implicit import statement. Only those members in the same package of the application can be accessed
using simple names.

MyBasicClass can access members using simple names in the java.lang package without an explicit
import statement. The compiler automatically imports the java.lang package.

MyBasicClass can access members using simple names in the same application without an explicit
import statement. If those members are in the same package, then the compiler will automatically
import them.

Given:

public class MyBasicClass {


//Insert code here
}

Which three lines of code can be included in the class?


package basicPackage;

import java.text.*;

enum ClassType {basic, advanced}

void BasicMethod() {}

static final int VAL=1000;

Explanation:
The three lines of code can be included in the class as follows:

public class MyBasicClass {


enum ClassType {basic, advanced}
void BasicMethod() {}
static final int VAL=1000;
}

A class body can include static and non-static fields, methods, constructors, and nested enumerations
and classes.

The code line package basicPackage; cannot be included in the class because a package statement
must be the first line in the source file, not inside a class body.

The code line import java.text.*; cannot be included in the class because import statements are
not allowed in class bodies.

Given:

public class CommandLineClass {


public static void main(String[] args) {
System.out.print("Argument: " + args[1]);
}
}

Which command will run the code without error?


java CommandLineClass arg1

java CommandLineClass arg1 arg2

javac CommandLineClass arg1

javac CommandLineClass arg1 arg2

Explanation:
The command java CommandLineClass arg1 arg2 will run the code without error. The java
command executes compiled Java files and sends arguments as a space-delimited list. The expression
args[1] retrieves the second argument, which is the value arg2 in this case.

The command java CommandLineClass arg1 will not run the code without error. Because the
second argument is missing, an ArrayOutOfBoundsException will be thrown.

The javac command will not run the code without error because this command compiles Java source
code.

Which two statements are true about the contents of a class?


A class can include only non-static members.
A class cannot include package or import statements.

A class can include nested enumerations and classes.

A class cannot include constructors.

Explanation:
A class can include nested enumerations and classes, but cannot include package or import statements.
A class body can include static and non-static fields, methods, constructors and nested enumerations and
classes.

A class can include both static and non-static members.

A class can include constructors. If no constructor is included, then the compiler will provide a
parameterless default constructor for the class.

Given:

package games.card;

public class Poker {


public void call() {System.out.println("Call!");}
public void raise(double amt) {System.out.println("Raise by " + amt);}
public void fold() {System.out.println("Fold!");}
}

and

package java7.app;

//Insert code here

public class GameApp {


public static void main(String[] args) {
Poker pokerGame = new Poker();
if (args.length > 0) {
if (args[0].equalsIgnoreCase("raise")) {
pokerGame.raise(Double.parseDouble(args[1]));
} else pokerGame.call();
} else pokerGame.fold();
}
}

Which two import statements, when inserted independently in the second source file, would enable the
code to compile and run?
import java7;

import java7.*;
import java7.app.*;

import java7.app.GameApp;

import games;

import games.*;

import games.card.*;

import games.card.Poker;

Explanation:
The import statements import games.card.*; and import games.card.Poker;, when inserted
independently in the second source file, would enable the code to compile and run. The import
statement import games.card.*; imports all classes within the games.card package, which
includes the fully qualified class games.card.Poker, while the import statement import
games.card.Poker; specifies only the Poker class in the games.card package.

The import statements import java7;, import java7.*;, import java7.app.*; and import
java7.app.GameApp; would not enable the code to compile and run. All of these import statements
reference the existing package for the GameApp class, not the package that contains the Poker class.
Also, the compiler will automatically import the current package.

The import statements import games; and import games.*; will not enable the code to compile
and run. These import statements import the classes in the games package, but not those classes in
the games.card package. The Poker class is in the games.card package.

10

Given:

public class VarScope {


static int var;
public static void main (String[] args) {
int var = 9;
printVar();
}
public static void printVar() {
int var = 10;
System.out.print("var = " + var++);
}
}

What is the result?


var = 0

var = 9
var = 10

var = 11

Explanation:
The result will be the output var = 10. The output is based on the local variable named var in the
printVar method. The variable var in this scope is set to 10, printed, and then incremented to 11.

The result will not be the output var = 0 because the local variable in the printVar method will be
printed. This result would be the output if the class variable var were printed because class and instance
variables are automatically initialized to their default values.

The result will not be the output var = 9 because the local variable in the printVar method will be
printed. This result would be the output if the local variable var in the main method were printed.

The result will not be the output var = 11 because the local variable in the printVar method will be
printed before it is incremented. This result would be the output if the prefix increment operator were
specified, not the postfix increment operator.

11

Given:

public class Circle {


static double getCircumference(double radius ) {
return PI * 2 * radius;
}
public static double getArea(double radius) {
return PI * radius * radius;
}
}

Which import statement will enable the code to compile and run?
import java.lang.*;

import java.lang.Math;

import java.lang.Math.*;

import static java.lang.Math.PI;

Explanation:
The following import statement will enable the code to compile and run:

import static java.lang.Math.PI;

A static import statement provides access by simple name to constants and static methods of a class. In
this case, the simple name PI can be used to refer to the PI constant in the java.lang.Math class.
The statement import java.lang.*; will not enable the code to compile and run. By default, the
compiler will automatically import the current package, java.lang package, and the package with no
name.

The statements import java.lang.Math; and import java.lang.Math.*; will not enable the
code to compile and run. These import statements will only allow Math.PI as a reference to the PI
constant. Because the java.lang package is automatically imported, these import statements are
unneeded.

12

Given the following command:

java CommandLineClass 1,2,3,4 5:6 7

When used in the main method, which expression will retrieve the argument 7?
args[2]

args[3]

args[6]

args[7]

Explanation:
The expression args[2] will retrieve the argument 7 when used in the main method. This expression
accesses the third argument in the space-delimited list specified by the java command.

The expression args[3] will not retrieve the argument 7 because this expression access the fourth
argument. An ArrayIndexOutOfBoundsException will be thrown because a fourth argument is not
provided in the java command.

The expressions args[6] and args[7] will not retrieve the argument 7 because arguments are only
space-delimited and the other characters are not valid delimiters.

You might also like