CSS 142 Multiple Choice Midterm

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/53

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

54 Terms

1
New cards

Can Java have two variables named name and Name?

Yes, always

2
New cards
3
New cards
4
New cards
5
New cards
6
New cards

What output is produced by:

String greetings = "Hello" + " there " + "earthling";
System.out.println(greetings);

Hello there earthling

7
New cards

Consider the following code:

System.out.println("Please tell us your name");

Scanner keyboardInput = new Scanner(System.in);

What can you do to make sure the input is read?

The code is incomplete

Needs to have something like

String fullName = userInput.nextLine();

8
New cards

There are only two basic types of methods..

Methods that returns a value and methods that do not.

9
New cards

What is the syntax for importing a scanner?

import java.util.*;

10
New cards

How do you declare a variable?

variableType variableName

i.e. double decimals

11
New cards

What does the following boolean expression evaluate to?

((a < 100) && (!(a == 66) || (a == 77)))

True whenever a is less than 100 and is not 66 (or equal to 77); false otherwise

12
New cards

How do you change a variable's type?

By casting,

int b = 0;

(double)b

13
New cards

How to import math?

What are methods you can use within math?

Don't need to import math

Math.abs();

Math.hypot(double x, double y)

Math.max/min(2 numbers);

Math.sqrt();

14
New cards

If boolean variable P is true and Q is false, what is !(P || Q)?

false

15
New cards

What is the value of e?

int a = 4;

int b = 3;

double c = 3.2;

double d = 1.0;

double e;

if ((a / b) < (b / a)) {

e = c / d;

} else {

e = a / b;

}

1.0

First, performs a/b as ints then converts the answer to double

16
New cards

Syntax for switch statements

switch (value being compared to)

{

case 1:

code

break;

case 2;

code

break;

}

when value is equal to case #, the statement executes

17
New cards

int n = 10;

do

{

System.out.println(n);

n = n - 3;

} while (n > 0);

10

7

4

1

18
New cards

What is the output of the following?

for (int count = 1; count < 5; count++)

System.out.print((2 * count) + " ");

2 4 6 8

19
New cards

public static void whileMystery(int x, int y, int z) {

while (x < z || y < z) {

x = x + y;

y = y * 2;

System.out.print(x + " " + y + " ");

}

System.out.println();

}

5 10 15 20

20
New cards

A while loop

will execute if its test condition is true.

21
New cards

What is the expression num *= 3 equivalent to?

num = num * 3;

22
New cards

The boolean, int, and double variable types are examples of

primitive types

23
New cards

A do-while loop

will always execute at least once.

24
New cards

a = false;

b = true;

c = 5 < 3;

x = !( a & b & c) == ( !a | !b | !c);

What is x equal to?

true

25
New cards

Data types of arrays can be

Of any data type, including an array data type

26
New cards

What is the correct way to create an empty integer array that contains 3 elements?

int[] arr = new int[3];

27
New cards

What is wrong with the following code?

int[] nums = 0;

0 is not a valid value for an integer array

28
New cards

What is the outcome of the following code?

int[] a = { 1 , 2, 3, 4, 5 };

int[] b = { 7, 8, 9, 10 };

b = a;

b[4] = 99;

System.out.println(a[4]);

99

29
New cards

A constructor

is executed when an instance is created

30
New cards

Instance variables

are class-level variables

31
New cards

Actions in a class object

are typically non-static methods

32
New cards

Assume a class "Cat" with a toString() method. Which of these would be an acceptable return value?

All of these are acceptable return values for a toString() method

The cat's name

A string that includes all of the Cat instance's properties

"Bob"

All of these are acceptable return values for a toString() method

33
New cards

Assume a class "Dog" with an equals() method.

Which of these would be an acceptable comparison between "this" Dog instance and "another" Dog instance?

Comparison of only Dog's memory locations

Comparison of all Dog's properties

All of these are acceptable comparisons between Dog instances

Comparison of only Dog's name

All of these are acceptable comparisons between Dog instances

34
New cards

Given a class "Ball" with a "radius" property and "getArea()" method, which of these would be an appropriate return statement for a toString() method?

Any of these return statements would be appropriate

return ("Area = " + getArea());

return ("This is a circle.");

return ("Radius = " + this.radius);

Any of these return statements would be appropriate

35
New cards

Which is likely a proper implementation of the "equals" method?

Comparing the values of instance variables that are of primitive data types

36
New cards

Given the following Class

public class LightSaber

{

private static String color;

public LightSaber(String c)

{

this.color = c;

}

}

Let's assume that we create the following in a Driver class:

LightSaber sith = new LightSaber("red");

LightSaber luke = new LightSaber("blue");

LightSaber yoda = new LightSaber("green");

What is the color of the "sith" light saber after the "yoda" light saber is created?

green

37
New cards

What is the main difference between the "while" loop and the "do/while" loop?

The do/while loop is guaranteed to "run" at least once

38
New cards

What is the outcome of the following code?

int sum = 0;

for(int i = 0; i < 10; i++)

{

for(int j = 0; j < 10; j++)

{

sum++;

}

}

sum is 100

39
New cards

int sum = 0;

for(int i = 0; i < 10; i++)

{

for(int j = 0; i < 10; i++)

{

sum++;

}

}

What is sum?

10

40
New cards

What will happen when the following code executes?

int sum = 0;

for(int i = 0; i < 10; i++)

{

for(int j = 0; j < 10; i++)

{

sum++;

}

}

endless loop

41
New cards

The this keyword

refers to the calling instance object.

42
New cards

How many elements is in an array thisArray?

thisArray.length;

43
New cards

A class that has a main method

can be a main program.

44
New cards

Consider the following code:

int[] a = {4, 7, 2, 1, -2};

int[] b = {a[0], a[1], a[2], a[3], a[4]};

a[2] = a[1] * 3;

b[1] = a[2] - 9;

What is the contents of a after this code is executed?

4, 7, 21, 1, -2

45
New cards

How many iterations will the following loop go through? (I.e., how many times is the loop body executed?)

for (int i = 2; (i > 1) && (i < 10); i = i+2)

{

System.out.println(i);

i = i - 1;

i = 10 / i;

}

1

46
New cards

Object instance variables

store data associated with the object.

47
New cards

A properly written accessor method of a class

returns the value of private instance variables.

48
New cards

Consider the following code:

int[] a = {4, 7, 2, 1, -2};

int[] b = a;

a[2] = a[1] * 3;

b[1] = a[2] - 9;

What is the contents of a after this code is executed?

4, -7, 21, 1, -2

49
New cards

The instance of a class

follows the structure set by the class definition.

50
New cards

Which of the following code snippets creates an instance of the class MyClass?

MyClass aMyClass = new MyClass();

51
New cards

Object methods

act on data connected to or passed into the object.

52
New cards

What is the difference(s) between a public and private instance variable?

Private instance variables cannot be directly read or set while public instances variables can be directly read and set.

53
New cards

Two classes that each have a method of the same name

will work fine.

54
New cards

A properly written mutator method of a class

sets the value of private instance variables.