Você está na página 1de 13

System.out.

print("Argument: " +
1. java basic args[2]);
1 }
Which statement is true about declaring members in a }
concrete class?
and
All methods and constructors must contain
bodies. javac CommandLineClass.java
All fields must be initialized explicitly. java CommandLineClass arg1,arg2 arg3

Only instance fields and methods are What is the result?


supported.
arg1
Only static fields and methods are supported.
arg2

2 arg3
Given:
arg1,arg2
public class VarScope {
int var; Exception in thread "main"
public static void main (String[] args) java.lang.ArrayIndexOutOfBoundsExcep
{ tion
int var = 10;
VarScope scope = new VarScope(); 5
scope.var = var + 2; Given:
scope.adjustVar(scope.var + 2);
System.out.println("var = " + var); package java7;
} public class MyBasicClass {}
private void adjustVar(int var) {
var += 2; Which statement is true about packages in
} MyBasicClass?
}
MyBasicClass can access members using
What is the result? simple names in the java7 package without
an implicit import statement.
var = 10
MyBasicClass can access all members using
var = 12 simple names in the same application without
an implicit import statement.
var = 14 MyBasicClass can only access members
using simple names in the java.lang
var = 16
package with an explicit import statement.
MyBasicClass can only access members
3 using simple names in the same application
Given: with an explicit import statement.
int i2 = i1 + 4;
System.out.println("i2 = " + i2);
6
Which line of code declares variable(s) so that the Given:
code fragment will compile?
public class MyBasicClass {
int i1; //Insert code here
}
int i1, i2;
Which three lines of code can be included in the class?
int i1 = 0; package basicPackage;

int i1 = 0, i2 = 0; import java.text.*;

enum ClassType {basic, advanced}


4
Given: void BasicMethod() {}

public class CommandLineClass { static final int VAL=1000;


public static void main(String[] args) {
1
7
Given: Which two import statements, when inserted
independently in the second source file, would enable
public class CommandLineClass { the code to compile and run?
public static void main(String[] args) { import java7;
System.out.print("Argument: " +
args[1]); import java7.*;
}
} import java7.app.*;
Which command will run the code without error? import java7.app.GameApp;
java CommandLineClass arg1
import games;
java CommandLineClass arg1 arg2
import games.*;
javac CommandLineClass arg1
import games.card.*;
javac CommandLineClass arg1 arg2
import games.card.Poker;

8
Which two statements are true about the contents of a 10
class? Given:
A class can include only non-static members. public class VarScope {
A class cannot include package or import static int var;
statements. public static void main (String[] args)
{
A class can include nested enumerations and int var = 9;
classes. printVar();
}
A class cannot include constructors.
public static void printVar() {
int var = 10;
9 System.out.print("var = " + var++);
Given: }
}
package games.card;
What is the result?
public class Poker { var = 0
public void call()
{System.out.println("Call!");} var = 9
public void raise(double amt)
{System.out.println("Raise by " + amt);} var = 10
public void fold()
{System.out.println("Fold!");} var = 11
}

and 11
Given:
package java7.app;
public class Circle {
//Insert code here static double getCircumference(double
radius ) {
public class GameApp { return PI * 2 * radius;
public static void main(String[] args) { }
Poker pokerGame = new Poker(); public static double getArea(double
if (args.length > 0) { radius) {
if (args[0].equalsIgnoreCase("raise")) { return PI * radius * radius;
pokerGame.raise(Double.parseDouble(args[ }
1])); }
} else pokerGame.call();
} else pokerGame.fold(); Which import statement will enable the code to
} compile and run?
}

2
import java.lang.*; manufacturer = "Oracle";

import java.lang.Math; this.manufacturer = "Oracle";

import java.lang.Math.*; super.manufacturer = "Oracle";

import static java.lang.Math.PI; Machine.manufacturer = "Oracle";


Machine myMachine = new Machine();
12 myMachine.manufacturer = "Oracle";
Given the following command:
3
java CommandLineClass 1,2,3,4 5:6 7 Given:
When used in the main method, which expression will public class Java7_I {
retrieve the argument 7? public static void main (String[] args)
args[2] {
int x = 1;
args[3] modifyVar(x + 5);
System.out.println("x: " + x);
args[6] }
public static void modifyVar(int var) {
args[7] var = 10;
} }

What is the result when this program is executed?


2. data type x: 1
1
Given: x: 6

1. public class Java7_I { x: 10


2. public static void main (String[]
args) { x: 16
3. //Code goes here
4. System.out.println("Value: " + b);
5. } 4
6. } Given:

Which declaration line, when inserted at line 3, public class Java7_I {


enables the code to compile and run? static class RefType {
int val;
boolean b = 0; RefType(int val) {this.val = val;}
}
boolean b = null;
public static void main (String[] args)
byte b = 1101; {
RefType x = new RefType(1);
byte b = 0b1101; RefType y = x;
RefType z = y;
z.val = 10;
2
System.out.format("x,y,z: %d,%d,%d",
Given the following class: x.val, y.val, z.val);
}
1. public class Machine {
}
2. static String manufacturer;
3. public static void main (String[]
What is the result when this program is executed?
args) {
4. //Insert code here x,y,z: 10,10,10
5. }
6. } x,y,z: 1,10,10

Which three code fragments correctly assign a value to x,y,z: 1,1,10


the manufacturer field at line 4?
x,y,z: 1,1,1

3
5
Given the following class: 8
Which two declaration statements correctly initialize
1. public class Machine { their variables?
2. float OSVersion;
3. public static void main (String[] boolean b1 = (6 < 4);
args) {
4. //Insert code here boolean b2 = 1;
5. }
6. } int i1 = 40.4;

int i2 = -6,000;
Which code fragment correctly assigns a value to the
OSVersion field at line 4?
float f1 = 10.01;
OSVersion = 10.1f;
float f2 = 10.01E2f;
Machine.OSVersion = 10.1f;
Machine myMachine = new Machine(); 9
myMachine.OSVersion = 10; Given:
Machine myMachine = new Machine();
Machine.OSVersion = 10; String str1 = "salt";
String str2 = "sAlT";
6 Which two code fragments will output str1 and
Given: str2 are equal?
StringBuilder sb = new if (str1 == str2 )
StringBuilder("Test"); System.out.println("str1 and str2
are equal");
What is the initial capacity of the StringBuilder if (str1.equals(str2) )
object? System.out.println("str1 and str2
are equal");
4
if (str1 == str2.toLowerCase() )
12 System.out.println("str1 and str2
are equal");
16 if (str1.equals(str2.toLowerCase())
)
20 System.out.println("str1 and str2
are equal");
7 if (str1.equalsIgnoreCase(str2) )
Given: System.out.println("str1 and str2
are equal");
public class Java7_I {
public static void main (String[] args) 10
{ Given:
StringBuilder sb = new
StringBuilder("Goodbye, Dog"); public class Java7_I {
sb.append("!"); public static void main (String[] args)
sb.replace(9,12,"Spot"); {
sb.insert(0,"Bad! "); int x = 1;
System.out.println(sb); int y = x;
} int z = y;
} z = 10;
System.out.format("x,y,z: %d,%d,%d", x,
What is the result when this program is executed? y, z);
Spot, Dog! }
}
Bad! GoodSpot, Dog!

Bad! Goodbye, Spot!

SpotBad! Goodbye, Dog!

4
What is the result when this program is executed? display();
x,y,z: 10,10,10
Customer.display();
x,y,z: 1,10,10
Customer().display();
x,y,z: 1,1,10
new Customer().display();
x,y,z: 1,1,1

14
11 Click the Exhibit(s) button. (see it at the next page!)
Given:
Which two lines of code will output Coord: 0,0
public class SmartPhone { Dim: 5 X 5?
float screenResolution, width, height;
public static void main (String[] args) Rectangle.display(5);
{
SmartPhone phone; Rectangle(5).display();
phone.height = 112.2f;
phone.width = 56.8f; new Rectangle(5,5,0,0).display();
System.out.format("%.0f dpi - %.1f X
%.1f", new Rectangle(5).display();
phone.screenResolution, phone.height,
phone.width); new Rectangle(0,0,5).display();
}
}
15
Given:
What is the result?
null dpi - 112.2 X 56.8 String str = "Goodbye, Dog";

0 dpi - 112.2 X 56.8 Which code fragment will output Good, Dog?
str.delete(4,8);
A runtime error is produced. System.out.println(str);
A compile error is produced. str.replace("bye","");
System.out.println(str);
String strNew =
12 str.replace("bye","");
Given: System.out.println(strNew);

public class Java7_I { String strNew = str.delete(4,8);


public static void main (String[] args) System.out.println(strNew);
{
StringBuilder sb = new 16
StringBuilder("Test"); Which statement is true about string manipulation?
System.out.println(sb.length());
String objects cannot be modified once they
}
are created.
}
String objects are stored as variable-length
What is the result when this program is executed? arrays of characters.
4 Manipulation methods in the StringBuilder
class create and return new String objects.
12
Manipulation methods in the String class
16 accept StringBuilder objects.

20 17
Given the following code fragment:
13 Customer cust = new Customer();
Click the Exhibit(s) button. (see it at the next page!)
Which part of the statement instantiates the Customer
Which line of code correctly invokes the display
object?
method from outside of the Customer class?
5
Exhibit 13

Exhibit 14

6
}
new
Which two values for the variable cardVal will output
cust Stand?
6
Customer cust
10
Customer()
14

16
3. operators and decision
18
construct
1
Which operator or method should determine whether
4
two String variables have the same value? Which statement is true about using a String object
in a switch statement?
==
String comparisons in case labels are case-
=== sensitive.
String comparisons in case labels are case-
equals insensitive.

contentEquals Execution falls through if break statements


are specified in case labels.
Execution terminates if break statements are
2 not specified in case labels.
Given:

public class Java7_I { 5


public static void main (String[] args) Given:
{
int i = 1; if ( x < 10) {
System.out.println((3 + i) * 6 ^ 2); if (x != 0)
} System.out.print("She");
} else
System.out.print("Sally");
What is the result when this program is executed? if (x < 5)
System.out.print(" sells seashells");
11
if ( x > 10)
System.out.print(" will sell all her
26
seashore shells");
else if (x < 15)
39.0
System.out.print(" by the");
else if (x < 20)
144.0
System.out.print(" on the");
if ( x < 10)
3 System.out.print(" seashore");
else
Given:
System.out.print(" seashell shore");
switch(cardVal) { } else {
case 4: case 5: case 6: System.out.print("Of that I'm sure");
case 7: case 8: }
System.out.println("Hit");
break; Which value for the variable x will output Sally
case 9: case 10: case 11: sells seashells by the seashore?
System.out.println("Double"); 0
break;
case 15: case 16: 1
System.out.println("Surrender");
break; 5
default:
System.out.println("Stand"); 10

7
6 if ( i >= 3)
Examine the code fragment: System.out.print("e ");
else if (i == 5) {
public static String getGradeFB (String System.out.print("= ");
grade) { } else {
String comment = null; System.out.print("!= ");
switch (grade) { if (i < 6)
case "A": System.out.print("mc2");
case "B": else
comment = "Excellent job!"; System.out.println("no energy");
break; }
case "C": if ( i >= 3) {
case "D": System.out.print("e ");
comment = "You should try again."; if (i < 6)
break; System.out.print("= ");
case "F": else
comment = "You should study more."; System.out.print("!= ");
default: if (i == 5)
throw new RuntimeException(); System.out.print("mc2");
} } else
return comment; System.out.println("no energy");
}
if ( i >= 3)
Which three code statements will throw an exception? System.out.print("e ");
if (i < 6)
getGradeFB("A"); System.out.print("= ");
else
getGradeFB("B"); System.out.print("!= ");
if (i == 5)
getGradeFB("C"); System.out.print("mc2");
else
getGradeFB("d"); System.out.println("no energy");
getGradeFB("F");
8
getGradeFB(null); Given:

int x = 5;
7
Given: Which two expressions evaluate to 5?

int i = 5; x-- + 10

- --x + 10
Which two code fragments will output e = mc2?
if ( i >= 3) -x + 10
System.out.print("e ");
else if (i == 5) 10 - --x
System.out.print("= ");
else if (i > 6) 10 - x--
System.out.print("!= ");
else if (i < 6) 10 - -x
System.out.print("mc2");
else
System.out.println("no energy"); 9
if ( i >= 3) Which code line increments the variable x after the
System.out.print("e "); variable y is assigned to the expression?
if (i == 5) { int y = (x + 1) * 25;
System.out.print("= ");
} else { int y = x + 1 * 25;
System.out.print("!= ");
if (i < 6) int y = x++ * 25;
System.out.print("mc2");
else int y = ++x * 25;
System.out.println("no energy");
}
8
10
Given: Which two expressions evaluate to 3?
(i + 5) - 6 * 10 / 5
if (x != 0)
System.out.print("She"); i + (5 - 6) * 10 / 5
else
System.out.print("Sally"); (i + 5 - 6) * 10 / 5
if (x < 5)
System.out.print(" sells seashells"); i + (5 - 6 * 10) / 5
if ( x > 10)
System.out.print(" will sell all her (i + 5) - 6 * (10 / 5)
seashore shells");
if (x < 15) ((i + 5 - 6) * 10) / 5
System.out.print(" by the");
else if (x < 20)
System.out.print(" on the");
if ( x < 10)
System.out.print(" seashore");
4. arrays
else
1
System.out.print(" seashell shore"); Given the following code fragment:

public class TestArrayList {


Which value for the variable x will output She will
public static void main (String[] args)
sell all her seashore shells on the
{
seashell shore? ArrayList<String> names = new
0 ArrayList<>(
Arrays.asList("Amy","Anne","Brian","Geor
10 ge","Ruth","Todd"));
names.add("Jason");
15 System.out.println(names[6]);
}
20 }

What is the result?


11
Given: Code compilation fails.

public class Java7_I { Code throws a runtime exception.


public static void main (String[] args)
Ruth
{
String s1 = "salty";
Todd
String s2 = new String("salty");
String s3 = s2;
Jason
if (s1.equals(s2) && s2.equals(s3))
System.out.println("We are equal!");
if (s1 == s2 && s2 == s3) 2
System.out.println("We are really Given:
equal!");
} 1. public class Java7_I {
} 2. public static void main (String[]
args) {
What is the result when this program is executed? 3. char[] src =
We are equal! 4. { 'j', 'e', 's', 'p', 'r', 'e', 's',
5. 's', 'o', 'a', 'v', 'a', '7' };
We are really equal! 6. char[] dest = new char[8];
7. //Insert code here
We are equal! 8. System.out.println(new String(dest));
We are really equal! 9. }
10. }
Nothing prints.
Which line of code, when inserted independently at
12 line 7, would output espresso?
Given: System.arraycopy(src, 8, dest, 0,
1);
int i = 10;
9
System.arraycopy(src, 8, dest, 0, matrix[[1][1][1]] = 42;
2);
System.arraycopy(src, 1, dest, 0,
8); 6
Given the code fragment:
System.arraycopy(src, 2, dest, 0,
8); char [][] charArray2D =
{{'c','u','p'},{'j','a','v','a'}};
3 System.out.println(charArray2D[1][2]);
Given the following code fragment:
What is the result?
public class TestArrayList { u
public static void main (String[] args)
{ v
ArrayList<String> names = new
ArrayList<>(2); cup
names.add("Amy");
names.add("Anne"); java
names.add("Jason");
System.out.println(names.get(3));
} 7
} Which statement is true about array declarations?
An array's dimension can be set without
What is the result? using the new keyword.
Code compilation fails. Curly braces can contain a comma-delimited
list of dimensions.
Code throws a runtime exception.
Square brackets can contain a comma-
Anne delimited list of values.
Square brackets can be placed after the data
Jason type or variable name.

4 8
Which statement is true about using ArrayList Given:
objects?
ArrayList objects require less memory than public class Java7_I {
public static void main (String[] args)
arrays.
{
ArrayList objects provide better int[10] intArray; //line 1
performance than arrays. intArray[0] = 10; //line 2
ArrayList objects require less low-level intArray[10] = 0; //line 3
System.out.print(intArray.length);
implementation than arrays.
//line 4
ArrayList objects are fixed in size and not }
resizable like arrays. }

5 Which line causes a compilation error?


Given: line 1

int[][][] matrix = new int[5][5][5]; line 2

Which line of code sets the first element of the matrix line 3
array?
line 4
matrix[0,0,0] = 42;

matrix[1,1,1] = 42; 9
Given:
matrix[0][0][0] = 42;
char[][] charArray2D =
matrix[1][1][1] = 42; {{'c','u','p'},{'o'},{'f'},{'j','a','v',
'a'}};
matrix[[0][0][0]] = 42;
10
s1.equals(s2)
Which two expressions will evaluate to false?
charArray2D.length > 4 s1.compareTo(s2)

charArray2D[0].length < 2 i = 2

charArray2D[1].length < 2 i == 2

charArray2D.getClass().isArray() b != "false"

charArray2D[0].getClass().isArray() i <= b

charArray2D[1].getClass().isArray()
4
Given:

5. loop int x = 0;
1
Given the code fragment: Which code fragment increments x to 10?
while (x < 10) { x++; }
int i = 0;
for (;;) { while (x < 11) { x++; }
i++;
} while (x < 10 ? 1 : 0) { x++; }
System.out.println(i);
while (x < 11 ? 1 : 0) { x++; }
What is the result?
Code compilation fails. 5
Given the following output:
Code throws a runtime exception.
8
0
5
2
2,147,483,647
-1
-2,147,483,648
Which code fragment generates this output?
int x = 10, y = 0;
2 do {
Which statement is true about the while statement in x += 2;
a do-while block? System.out.println(y - x);
y --;
The while statement requires an expression } while ( y - x > 0 );
that must evaluate to an int.
int x = 0, y = 10;
The while statement requires an expression do {
that must evaluate to a boolean. x += 2;
Statements within a while block will execute System.out.println(y - x);
y --;
at least once.
} while ( y - x > 0 );
Statements within a do-while block may
int x = 10, y = 0;
never execute.
do {
x += 2;
3 System.out.println(x - y);
Given: y --;
} while ( x - y > 0 );
String s1 = "espresso"; int x = 0, y = 10;
String s2 = "java"; do {
int i = 1; x += 2;
boolean b = true; System.out.println(x - y);
y --;
Which two expressions are valid in a while } while ( x - y > 0 );
statement?

11
6 9
Given the code fragment:
10
char[] charArray = { '8', '9', '0', 'e',
's', 'p', 'r', 'e', 's', 's', 'o'};
int i = 48; //Start range for digits 9
do { Given:
for(char c : charArray) {
if ((char) i == c) { public class Java7_Looping {
System.out.println(c + " found!"); public static void main(String[] args) {
//insert skip statement char[] charArray = { 'e', 's', 'p', 'r',
} 'e', 's', 's', 'o', '8', '9', '0'};
} int i = 48; //Start range for digits
} while (i++ < 57); //End range for do {
digits for(char c : charArray)
if ((char) i == c)
The skip statement should terminate only the inner System.out.println(c + " found!");
for block if a numerical character is found. } while (i++ < 57); //End range for
digits
Which skip statement should be inserted in the code? }
}
break;

break for; How many times is found! printed?


None
continue;
Once
return;
Twice
7 Thrice
Given the code fragment:

int[] grades = {73,82,97,91,67}; 10


Given the following output:
Which two sets of expressions are valid in a for
statement? i: 0
i: 3
;; i++
i: 6
;i < 5; i++
Which three code fragments generate this output?
int i = 0;i < 5; i++ for (int i = 0; i < 8;) {
System.out.println("i: " + i);
int i : grades i += 3;
}
grades : int i for (int i = 0; i < 8; i += 3) {
System.out.println("i: " + i);
}
8
for (int i = 0; i < 8; i += 2) {
Given the code fragment:
System.out.println("i: " + i);
int i = 0; i++;
while (i < 10 ? 1 : 0) { }
i++; for (int i = 0; i < 8; i++) {
} i += 2;
System.out.println(i); System.out.println("i: " + i);
}
What is the result? for (int i = 0; i < 8;) {
Code compilation fails. i += 3;
System.out.println("i: " + i);
Code throws a runtime exception. }
for (int i = 0; i < 8; i += 2) {
0 i++;
System.out.println("i: " + i);
12
} 11

11 An infinite number
Given:

public class Java7_Looping {


public static void main(String[] args) {
int intArray[] = { 1, 2, 3, 4, 5};
int index = 0;
do {
while (index < 10)
System.out.print(index++ + " ");
} while (index < intArray.length);
}
}

What is the result?


Nothing is printed.

0 1 2 3 4

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
8 9

12
Given the following code fragment:

int i = 6;
do {
System.out.print(--i + " ");
} while (i > 0);
System.out.print("...BLAST OFF!");

What is the result?


...BLAST OFF!

4 3 2 1 0 ...BLAST OFF!

5 4 3 2 1 0 ...BLAST OFF!

5 4 3 2 1 ...BLAST OFF!

6 5 4 3 2 1 ...BLAST OFF!

13
Given the code fragment:

for (int x = 0; x < 10; x--) {


do {
System.out.println("Loop!");
System.out.println("x:" + x);
} while (x++ < 10);
}

How many times is Loop! printed?


9

10

13

Você também pode gostar