CSCI 1301 final exam study guide

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

1/52

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.

53 Terms

1
New cards

Which of the following declares a variable that will store a measurement with fractional parts?

A) int measure;
B) double measure;
C) String measure;
D) integer measure;

B) double measure;

2
New cards

Assuming that the user inputs "Joe" at the prompt, what is the output of the following code snippet?

A) The code snippet does not compile because the += operator cannot be used in this context
B) Joe, Good morning
C) , Good morning
D) Joe

B) Joe, Good morning

3
New cards

The assignment operator

A) denotes mathematical equality
B) places a new value into a variable
C) means the same as the equal sign used in algebra
D) makes it illegal to write a statement like "sum = sum + 4"

B) places a new value into a variable

4
New cards

What term is used to refer a to a sequence of characters enclosed in quotation marks?
A) string
B) object
C) comment
D) variable

A) string

5
New cards

What is the result of the following statement
String s = "You" + "had" + "me" + "at" + "hello";

A) The string s has the following value: "You had me at hello"
B) The statement results in an error because the + operator can be only used with numbers
C) The statement results in an error because the + operator cannot be performed on string literals
D) The string s has the following value: "Youhadmeathello"

D) The string s has the following value: "Youhadmeathello"

6
New cards

A method may be called by specifying which 3 items in the specified order?

Object, method name, parameters

7
New cards

When a Java application starts, what is the name of the method that is executed?

A) main
B) start
C) begin
D) Main

A) main

8
New cards

What will be printed by the statement below
int x = 20;
int y = x;
x += 3;
y *= 2;
System.out.print("x = " + x + ", y = " + y);

A) x = 23, y = 46
B) x = 23, y = 40
C) x = 46, y = 46
D) x = 3, y = 2

B) x = 23, y = 40

9
New cards

Assuming the following Java statement

Circle cl = new Circle (3);

What does the variable c1 store?

A) The constructed object itself
B) A reference to the circle class
C) A reference to the memory location of the constructed object
D) The numeric value 3

C) A reference to the memory location of the constructed object

10
New cards

Assume a, b, and c have been defined to be integer variable. Which of the following value makes the expression below true?

! (a == b) && (c > a)

A) a = 10, b = 10, c = 15
B) a = 10, b = 20, c = 15
C) a = 10, b = 2, c = 5
D) a = 10, b = 20, c = 10

B) a = 10, b = 20, c = 15

11
New cards

Parameters and local variables belongs to ________________

Methods

12
New cards

Instances variables belong to ________________

Objects

13
New cards

static variable belong to ________________

Classes

14
New cards

Which of the following condition is true only when the integer variable "number" is even

A) number / 2 == 0
B) number > 2
C) number / 2 > 1
D) number % 2 == 0

D) number % 2 == 0

15
New cards

What is the output of the following code snippet
public static void main (string [] args) {
int value = 25;
value = value * 2;
value - -;
System.out println (value)
}

A) 25
B) 50
C) 49
D) 26

C) 49

16
New cards

Which of the following conditions is true only when the integer variable"middle" is between the values 0 and 10?

A) (0 <= middle) && (middle <= 10)
B) 0 <= middle <= 10
C) (0 <= middle) || (middle <= 10)
D) (0 <= 10) && (middle <= 10)

A) (0 <= middle) && (middle <= 10)

17
New cards

What is the name of file declaring the class named MyClass

A) MyClass
B) myclass.java
C) MyClass.class
D) MyClass.java

D) MyClass.java

18
New cards

Text enclosed between this and the end of line is ignored by the compiler.

A) "
B) //
C) ()
D) ""

B) //

19
New cards

What will be the range of the random numbers generated by the following code snippet?

Random generator = new Random();
int r1 = generator.nextInt(50) + 1;

A) Between 1 and 49
B) Between 0 and 50
C) Between 0 and 49
D) Between 1 and 50

D) Between 1 and 50

20
New cards

How many times does the following code fragment display "Hi"?

int i = 10;
while (i >= 0) {
System.out.println("Hi");
i- -;
}

A) 9 times
B) 10 times
C) 11 times
D) 12 times

C) 11 times

21
New cards

In Java, the symbol "=" and "==" are used synonymously (interchangeably)

A) True
B) False

B) False

22
New cards

What is the output of the code snippet given below?

int i = 0
while (i != 9) {
System.out.println("" + i);
i = i + 2;
}

A) No output
B) 0 2 4 6 8
C) 10 12 14 16 18 ...... (infinite loop)
D) 0 2 4 6 8 10 12 14 ...... (infinite loop)

D) 0 2 4 6 8 10 12 14 ...... (infinite loop)

23
New cards

How many times will the following loop run

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

A) 0
B) 8
C) 9
D) 10

D) 10

24
New cards

Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following?

if (count != 0 && total / count > max) {
max = total / count;
}

A) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
B) The condition short circuits and the assignment is executed without problems
C) The condition will not compile because it uses improper syntax
D) The condition does not short circuit causing a division by zero error
E) The condition short circuit and the assignment statement is not executed

E) The condition short circuit and the assignment statement is not executed

25
New cards

Select the statement that correctly completes the loop in this code snippet.

int years = 20;
double balance = 10000;
while (years > 0) {
__________________
double interest = balance * rate / 100;
balance = balance + interest;
}

A) years ++;
B) years - -;
C) balance ++;
D) balance - -;

B) years - -;

26
New cards

Given the nested if-else structure below:
if (a > 0) {
if (b < 0) {
x = x + 5;
}
else {
if (a > 5) {
x = x + 4;
}
else {
x = x + 3;
}
else {
x = x + 2;
}
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?

A) 3
B) 5
C) 2
D) 4

C) 2

27
New cards

How many times does the following code snippet display "Hello"?

int i = 0;
while (i != 15) {
System.out.println("Hello");
i++;
}

A) Infinite times
B) 14 times
C) 15 times
D) 16 times

C) 15 times

28
New cards

What is the output of the following code fragment?

int i = 1;
int sum = 0;
while (i <= 11) {
sum = sum + i;
i++
}
System.out.println("The value of sum is "+ sum) ;

A) The value of sum is 65
B) The value of sum is 66
C) The value of sum is 55
D) The value of sum is 56

B) The value of sum is 66

29
New cards

Given the nested if-else structure below; answer question below.

if (a>0) {
if (b<0) {
x=x+5;
}
else {
if (a>5) {
x=x+4;
}
else {
x=x+3;
}
else {
x=x+2;
}

If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?

A) 5
B) 4
C) 3
D)
E) 2

A) 5

30
New cards

What is the output of the code snippet given below?

int i;
int j = 0;
for (i = 0; i < 5; ++i) {
if (i % 2 == 0) {
i = i + 2;
j++
}
else {
i++
j = j + 2
}
j++
}
System.out.println("i=" + i + ", j=" + j);

A) i=7, j=7
B) i=7, j=6
C) i=6, j=7
D) i=5, j=5

D) i=5, j=5

31
New cards

What is the output of this code snippet

String str = "ABCabc";
char ch;
int i = 0
while (i < str.length() ) {
ch = str.charAt (i);
if (Character.isLowerCase (ch)) {
System.out.print(i + " ");
}
else {
i++
}
}

A) 3 4 5
B) 3
C) 3 3 3 3 3 ...... (infinite loop)
D) 0 1 2

C) 3 3 3 3 3 ...... (infinite loop)

32
New cards

How many times does the loop execute in the following code fragment?

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

A) 11
B) 12
C) 13
D) 14

C) 13

33
New cards

What will be printed by the statement below?

int a = 10;
while (a > 5) {
a = a - 2;
System.out.print(a + " ");
}

A) 10 8 6
B) 10 8 6 4
C) 8 6
D) 8 6 4

D) 8 6 4

34
New cards

What output does the "while" loop generate?

j = 6
while (j > 0) {
System.out.print(j + ", ");
j - -;
}

A) No output is generated
B) 6, 5, 4, 3, 2, 1
C) 6, 5, 4, 3, 2, 1,
D) The output is infinite

C) 6, 5, 4, 3, 2, 1,

35
New cards

What will be printed by the statement below?

for (int ctr = 10; ctr > 5; ctr - -) {
System.out.print(ctr + " ") ;
}

A) 10 9 8 7 6 5
B) 10 9 8 7 6
C) 5 6 7 8 9 10
D) 6 7 8 9 10

B) 10 9 8 7 6,

36
New cards

Given the following switch statement where x is an integer

switch (x) {
case 3: x += 1;
case 4: x += 2;
case 5: x += 3;
case 6: x++
case 7: x += 2
case 8: x--
case 9: x++

If x is currently equal to 5, what will the value of x be after the switch statement executes?

A) 8
B) 11
C) 5
D) 6
E) 10

B) 11

37
New cards

Which of the following conditions can be added to the code below so it will loop until the user enters "no" or "NO"?

Scanner in = new Scanner (System.in);
System.out.print("Continue? ");
String response = in.next();
while (/ put condition here /) {
System.out.println("Hello beautiful!");
System.out.print("Continue? ");
response = in.next();
}

A) response.equals("no") != response.equals("NO")
B) ! (response.equals("no") || response.equals("NO"))
C) ! response.equals("no") || ! response.equals("NO")
D) ! (response.equals("no") && response.equals("NO"))

B) ! (response.equals("no") || response.equals("NO"))

38
New cards

What does the following loop compute?

Scanner in = new Scanner (System.in);
int sum = 0;
int count = 0;
while (in.hasNextInt()) {
int value = in.nextInt();
if (value > 0) {
sum += value;
count ++;
}
}
double result = (sum * 1.0) / count;

A) The average of all the integers in the count
B) The sum of all the positive integers in the input divided by the number of integers in the input
C) The average of all the positive integers in the input
D) The second smallest value in the input

C) The average of all the positive integers in the input

39
New cards

The statement below can be rewritten using a conditional operator as

if (x < 0) y = x; else y = 0

A) x = (x < 0) ? y : 0;
B) y = if (x < 0) x : 0;
C) (x < 0) ? y = x : y = 0;
D) y = (x < 0) ? x : 0;
E) y = (x < 0);

D) y = (x < 0) ? x : 0;

40
New cards

In order to create a constant, you would use which of the following Java reserved words?

A) static
B) class
C) int
D) final
E) private

D) final

41
New cards

For the question below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all

Which of the following code could be used to compute the total number of bars sold by the children?

A) for (int j=0; j<12; j++) candy[j] = sum;
B) for (int j=0; j<12; j++) sum += candy [j];
C) for (int j=0; j<12; j++) sum += [j];
D) for (int j=0; j<12; j++) sum = candy [j];
E) for (int j=0; j<12; j++) [j] += sum;

B) for (int j=0; j<12; j++) sum += candy [j];

42
New cards

The "for" each loop

A) is convenient for traversing all elements in an array
B) is convenient for traversing elements in a partially filled array
C) is only used for arrays of integers
D) is used to initialize all array elements to a common value

A) is convenient for traversing all elements in an array

43
New cards

Assume the following variable has been declared and given a value as shown:

int [] numbers = {9, 17, -4, 21 };

What is the value of "numbers.length"?

A) 3
B) 4
C) 9
D) 21

B) 4

44
New cards

What is the result of executing this code snippet?

int [] marks = { 90, 45, 67 };
for (int i = 0; i <= 3; ++i) {
System.out.println (marks [i]);
}

A) The code snippet does not give any output.
B) The code snippet displays all the marks stored in the array without any redundancy.
C) The code snippet causes a bounds error.
D) The code snippet executed an infinite loop

C) The code snippet causes a bounds error.

45
New cards

What does the following code do? Assume list is an array of int values, temp is some previously initialized value, and c is an int initialized to 0

for (int j = 0; j < list.length; j++) {
if (list[j] < temp) {
c++;
}
}

A) It finds the largest value and stores it in temp
B) It sorts the values in list to be in ascending order
C) It finds the smallest value and stores it in temp
D) It counts the number of elements in list that are less than temp
E) It counts the number of elements equal to the smallest value in list

D) It counts the number of elements in list that are less than temp

46
New cards

A programmer compares x == y, where x and y are doubles. Many different values are excepted for x and y. For values that a programmer expects to be equal, the comparison will

A) always evaluate to true
B) always evaluate to false
C) sometimes evaluate to true, sometimes to false, depending on the values
D) sometimes immediately exit the program, for values that are slightly different

C) sometimes evaluate to true, sometimes to false, depending on the values

47
New cards

What is the output, if the input is 3 2 1 0 ?

x = scnr.nextInt();
while (x > 0) {
System.out.print(2 * x + " ");
}

A) 6
B) 6 4 2
C) 6 4 2 0
D) 6 6 6 6 6 ..... (infinite loop)

D) 6 6 6 6 6 ..... (infinite loop)

48
New cards

A loop should output 1 to n. If n is 5, the output is 12345. What should XXX and YYY be? Choices are in the form XXX / YYY.

n = scnr.nextInt()
for (XXX; i++) {
System.out.println(YYY);
}

A) i = 0; i < n / i
B) i = 0; i < n / i + 1
C) i = 1; i < n / i
D) i = 1; i < n / i

B) i = 0; i < n / i + 1

49
New cards

What is the output if count is 4?

for (i = count; i > 0; - - i) {
// output i
}

A) 4
B) 321
C) 4321
D) 43210

C) 4321

50
New cards

How many x's will be output? Assume row and col are integers.

for (row = 0; row < 2; ++row) {
System.out.print ("x");
for (col = 0; col < 3; ++col) {
// do something
}
}

A) 1
B) 2
C) 3
D) 6

B) 2

51
New cards

What is the output, if userVal is 5

int x;
x = 100;
if (userVal != 0) {
int tmpVal;
tmpVal = x / userVal
System.out.print (tmpVal);
}

A) 0
B) 5
C) 20
D) Error: The compiler generates an error

C) 20

52
New cards

What is the ending value of the element at index 1?

int [] numbers = new int [10];
numbers [0] = 35;
numbers [1] = 37;
numbers [1] = numbers[0] + 4;

A) 0
B) 35
C) 37
D) 39

D) 39

53
New cards

Which assigns the array's first element with 99?

int [] myVector = new int [4]

A) myVector[] = 99
B) myVector[-1] = 99
C) myVector[0] = 99
D) myVector[1] = 99

C) myVector[0] = 99