Finals Study Guide (Multiple Choice Section) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

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

A

System.out.println(“Welcome to Java”);

System.out.print(“Welcome to Java”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

________ is the brain of a computer.

A

CPU

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Every letter in a Java keyword is in lowercase.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

final double MAX_LENGTH = 99.98;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

Hardware

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

________is interpreted.

A

Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A block is enclosed inside ________.

A

braces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The main method header is written as:

A

public static void main(String[ ] args)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

2.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

________ is the Java assignment operator.

A

=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

Programs

Software

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

int length, width;

int length; int width;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

class
$343
radius

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

Test.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

One byte has ________ bits.

A

8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

To assign a value 1 to variable x, you write

A

x = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Every statement in Java ends with ________.

A

a semicolon (;)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A

radius

findArea

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Suppose a Scanner object is created as follows:

Scanner input = new Scanner(System.in);

What method do you use to read an int value?

A

input.nextInt();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
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

a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

bbb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Analyze the following code:

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

The equal comparison operator in Java is ________.

A

==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

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

}

A

Income is greater than 3000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Analyze the following code.

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

A

The code displays nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

In Java, the word true is ________.

A

a Boolean literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

The following code displays ________.

double temperature = 50;

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

just right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

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

x 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

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

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

10

35
Q

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

A

Yes

36
Q

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

10

37
Q

What is the output for y?

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

45

38
Q

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

A

between ‘a’ and ‘z’

39
Q

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

A

a local variable

40
Q

All Java applications must have a method ________.

A

public static void main(String[ ] args)

41
Q

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

The program displays long followed by 5.

42
Q

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

The program displays int, long followed by 5.

43
Q

Arguments to methods always appear within ________.

A

parentheses

44
Q

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

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

A

No

45
Q

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

No

46
Q

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

a stack

47
Q

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)?

A

invalid call

48
Q

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

A

2

49
Q

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

A

void

50
Q

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

A

information hiding

51
Q

The signature of a method consists of ________.

A

method name and parameter list

52
Q

What is k after the following block executes?

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

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

53
Q

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

A

pass by value

54
Q

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

A
  1. 5

0. 0

55
Q

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

A

Write a method that prints integers from 1 to 100.

56
Q

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

A

a class instance

57
Q

An object is an instance of a ________.

A

class

58
Q

Analyze the following code.

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

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

59
Q

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

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

60
Q

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

A

x contains a reference to a Circle object.

61
Q

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

A

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

62
Q

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

A

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

63
Q

The keyword ________ is required to declare a class.

A

class

64
Q

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

A

Welc me t Java

65
Q

What is the output of the following code?

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

A

University

66
Q

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

s1 and s2 reference to the same String object

67
Q

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

s1 and s2 reference to different String objects

68
Q

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

s1 and s2 have the same contents

69
Q

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

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

70
Q

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

A

the reference of the object

71
Q

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

A

A reference variable refers to an object.

An object may contain the references of other objects.

72
Q

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

A

String s = “Welcome to Java”;

73
Q

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

A

A class

74
Q

________ is invoked to create an object.

A

A constructor

75
Q

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

A

An object

76
Q

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

A

different methods in a class

77
Q

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

A

inheritance

78
Q

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

A

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
Q

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

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

80
Q

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

“The default constructor of A is invoked”“The default constructor of B is invoked”

81
Q

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

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

82
Q

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

m(new Person()) causes an error

m(new Object()) causes an error

83
Q

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

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.