Finals Study Guide (Multiple Choice Section) Flashcards Preview

Computer Science 111 > Finals Study Guide (Multiple Choice Section) > Flashcards

Flashcards in Finals Study Guide (Multiple Choice Section) Deck (83)
Loading flashcards...
1

Which of the following statements is correct to display Welcome to Java on the console? (Choose all that apply.)

System.out.println("Welcome to Java");
System.out.print("Welcome to Java");

2

________ is the brain of a computer.

CPU

3

Every letter in a Java keyword is in lowercase.

false

4

To declare a constant MAX_LENGTH inside a method with value 99.98, you write

final double MAX_LENGTH = 99.98;

5

________ is the physical aspect of the computer that can be seen.

Hardware

6

________is interpreted.

Java

7

Which of the following assignment statements is incorrect? (Choose all that apply.)

i = 1 = j = 1 = k = 1;
i = 1; j = 1; k = 1;
i == j == k == 1;

8

A block is enclosed inside ________.

braces

9

The main method header is written as:

public static void main(String[ ] args)

10

If you enter 1 2 3, when you run this program, what will be the output?

import java.util.Scanner;

public class Test1 {
public static void main(String[ ] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display result
System.out.println(average);
}
}

2.0

11

________ is the Java assignment operator.

=

12

________ are instructions to the computer. (Choose two.)

Programs
Software

13

Which of the following are correct ways to declare variables? (Choose two.)

int length, width;
int length; int width;

14

Which of the following is a valid identifier? (Choose all that apply.)

class
$343
radius

15

Suppose you define a Java class as follows:

public class Test {

}

In order to compile this program, the source code should be stored in a file named

Test.java

16

One byte has ________ bits.

8

17

To assign a value 1 to variable x, you write

x = 1;

18

Every statement in Java ends with ________.

a semicolon (;)

19

Which of the following are correct names for variables according to Java naming conventions? (Choose all that apply.)

radius
findArea

20

Suppose a Scanner object is created as follows:

Scanner input = new Scanner(System.in);

What method do you use to read an int value?

input.nextInt();

21

What is 1 + 1 + 1 + 1 + 1 == 5?

true

22

What is the printout of the following switch statement?
char ch = 'a';
switch (ch) {
case 'a':
case 'A':
System.out.print(ch); break;
case 'b':
case 'B':
System.out.print(ch); break;
case 'c':
case 'C':
System.out.print(ch); break;
case 'd':
case 'D':
System.out.print(ch);
}

a

23

What is the printout of the following switch statement?

char ch = 'b';

switch (ch) {
case 'a':
System.out.print(ch);
case 'b':
System.out.print(ch);
case 'c':
System.out.print(ch);
case 'd':
System.out.print(ch);
}

bbb

24

Analyze the following code:

if (x < 100) && (x > 10)
System.out.println("x is between 10 and 100");

The statement has compile errors because (x 10) must be enclosed inside parentheses.

25

The equal comparison operator in Java is ________.

==

26

Suppose income is 4001, what is the output of the following code:

if (income > 3000) {
System.out.println("Income is greater than 3000");
}
else if (income > 4000) {
System.out.println("Income is greater than 4000");

}

Income is greater than 3000

27

Analyze the following code.

boolean even = false;
if (even) {
System.out.println("It is even!");
}

The code displays nothing.

28

Which of the following code displays the area of a circle if the radius is strictly positive?

if (radius > 0) System.out.println(radius * radius * 3.14159);

29

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

((x < 100) && (x > 1)) || (x < 0)

30

In Java, the word true is ________.

a Boolean literal

31

The following code displays ________.

double temperature = 50;

if (temperature >= 100)
System.out.println("too hot");
else if (temperature );

just right

32

Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?

if (x > 0)
if (y > 0)
System.out.println("x > 0 and y > 0");
else if (z > 0)
System.out.println("x 0");

x 0;

33

Which of the Boolean expressions below is incorrect? (Choose three.)

(x != 0) || (x = 0)
(true) && (3 => 4)
(-10 < x < 0)

34

How many times will the following code print "Welcome to Java"?

int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}

10

35

Do the following two statements in (I) and (II) result in the same value in sum?

(I):
for (int i = 0; i<10; i++) {
sum += i;
}

Yes

36

How many times will the following code print "Welcome to Java"?

int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);

10

37

What is the output for y?

int sum = 0;
for (int i = 0; i<10; ++i) {
sum += i;
}
System.out.println(sum);

45

38

(char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character ________.

between 'a' and 'z'

39

A variable defined inside a method is referred to as ________.

a local variable

40

All Java applications must have a method ________.

public static void main(String[ ] args)

41

Analyze the following code:

class Test {
public static void main(String[ ] args) {
System.out.println(xmethod(5));
}

public static int xmethod(int n, long t) {
System.out.println("int");
return n;
}

public static long xmethod(long n) {
System.out.println("long");
return n;
}
}

The program displays long followed by 5.

42

Analyze the following code:

public class Test {
public static void main(String[ ] args) {
System.out.println(xMethod(5, 500L));
}

public static int xMethod(int n, long l) {
System.out.println("int, long");
return n;
}

public static long xMethod(long n, long l) {
System.out.println("long, long");
return n;
}
}

The program displays int, long followed by 5.

43

Arguments to methods always appear within ________.

parentheses

44

Does the method call in the following method cause compile errors?

public static void main(String[ ] args) {
Math.pow(2, 4);
}

No

45

Does the return statement in the following method cause compile errors?

public static void main(String[ ] args) {
int max = 0;
if (max != 0)
System.out.println(max);
else
return;
}

No

46

Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as ________, which stores elements in last-in first-out fashion.

a stack

47

Given the following method

static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}

What is the printout of the call nPrint('a', 4)?

invalid call

48

Given the following method

static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}

What is k after invoking nPrint("A message", k)?

int k = 2;
nPrint("A message", k);

2

49

Suppose your method does not return any value, which of the following keywords can be used as a return type?

void

50

The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as ________. (Choose all that apply.)

information hiding

51

The signature of a method consists of ________.

method name and parameter list

52

What is k after the following block executes?

{
int k = 2;
nPrint("A message", k);
}
System.out.println(k);

k is not defined outside the block. So, the program has a compile error

53

When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as ________.

pass by value

54

Which of the following is a possible output from invoking Math.random()? (Choose all that apply.)

0.5
0.0

55

Which of the following should be declared as a void method?

Write a method that prints integers from 1 to 100.

56

A method that is associated with an individual object is called ________.

a class instance

57

An object is an instance of a ________.

class

58

Analyze the following code.

class Test {
public static void main(String[ ] args) {
String s;
System.out.println("s is " + s);
}
}

The program has a compilation error because s is not initialized, but it is referenced in the println statement.

59

Analyze the following code.

public class Test {
int x;

public Test(String t) {
System.out.println("Test");
}

public static void main(String[ ] args) {
Test test = new Test();
System.out.println(test.x);
}
}

The program has a compile error because Test does not have a default constructor.

60

Given the declaration Circle x = new Circle(), which of the following statement is most accurate?

x contains a reference to a Circle object.

61

Given the declaration Circle[ ] x = new Circle[10], which of the following statement is most accurate?

x contains a reference to an array and each element in the array can hold a reference to a Circle object.

62

Suppose s is a string with the value "java". What will be assigned to x if you execute the following code?

char x = s.charAt(4);

Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

63

The keyword ________ is required to declare a class.

class

64

What is displayed by the following code?

public static void main(String[ ] args) throws Exception {
String[ ] tokens = "Welcome to Java".split("o");
for (int i = 0; i

Welc me t Java

65

What is the output of the following code?

String s = "University";
s.replace("i", "ABC");
System.out.println(s);

University

66

What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = "Welcome to Java!";
String s2 = "Welcome to Java!";

if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}

s1 and s2 reference to the same String object

67

What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");

if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}

s1 and s2 reference to different String objects

68

What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");

if (s1.equals(s2))
System.out.println("s1 and s2 have the same contents");
else
System.out.println("s1 and s2 have different contents");
}
}

s1 and s2 have the same contents

69

What is wrong in the following code?

class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}

public class C {
public static void main(String[ ] args) {
TempClass temp = new TempClass(2);
}
}

The program has a compilation error because TempClass does not have a constructor with an int argument.

70

When invoking a method with an object argument, ________ is passed.

the reference of the object

71

Which of the following statements is most accurate? (Choose two.)

A reference variable refers to an object.
An object may contain the references of other objects.

72

Which of the following statements is preferred to create a string "Welcome to Java"?

String s = "Welcome to Java";

73

________ is a construct that defines objects of the same type.

A class

74

________ is invoked to create an object.

A constructor

75

________ represents an entity in the real world that can be distinctly identified.

An object

76

You can declare two variables with the same name in ________.

different methods in a class

77

Object-oriented programming allows you to derive new classes from existing classes. This is called ________.

inheritance

78

Which of the following statements are true? (Choose two.)


A subclass is usually extended to contain more functions and more detailed information than its superclass.

"class A extends B" means A is a subclass of B.

79

Suppose you create a class Cylinder to be a subclass of Circle. Analyze the following code:

class Cylinder extends Circle {
double length;

Cylinder(double radius) {
Circle(radius);
}
}

The program has a compile error because you attempted to invoke the Circle class's constructor illegally.

80

What is the output of running class C?

class A {
public A() {
System.out.println("The default constructor of A is invoked");
}
}

class B extends A {
public B() {
System.out.println("The default constructor of B is invoked");
}
}

public class C {
public static void main(String[ ] args) {
B b = new B();
}
}

"The default constructor of A is invoked""The default constructor of B is invoked"

81

Analyze the following code:

public class Test {
public static void main(String[ ] args) {
B b = new B();
b.m(5);
System.out.println("i is " + b.i);
}
}

class A {
int i;

public void m(int i) {
this.i = i;
}
}

class B extends A {
public void m(String s) {
}
}

The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

82

Given the following code, find the compile error. (Choose two.)

public class Test {
public static void main(String[ ] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}

public static void m(Student x) {
System.out.println(x.toString());
}
}

class GraduateStudent extends Student {
}

class Student extends Person {
public String toString() {
return "Student";
}
}

class Person extends Object {
public String toString() {
return "Person";
}
}

m(new Person()) causes an error


m(new Object()) causes an error

83

Analyze the following code: (Choose two.)

public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
}
}

class A {
int x;
public String toString() {
return "A's x is " + x;
}
}

When executing System.out.println(a2), the toString() method in the Object class is invoked.


When executing System.out.println(a1), the toString() method in the A class is invoked.