Intro to Comp Sci II CPSC 1220, Comp 1220 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which input value causes the loop body to execute a 2nd time, thus outputting "In loop" again? string s = "Go"; while ((s != "q") && (s != "Q")){printf("In loop\n");scanf("%s", s);}

"Quit"

What is the output if count is 4? for (i = count; i > 0; --i) {// Output i}

4321

What is the index of the last element? int numList[50];

49

What is output, if the input is 3 2 1 0? scanf("%d", &x);while (x > 0) {printf("%d ", 2 * x);}

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

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. scanf("%d", &n);for (XXX; i++) {printf("%d", YYY);}

<code>i = 0; i < n / i + 1;</code>

Which XXX causes every character in string inputWord to be output? for (XXX) {printf("%c\n" &inputWord[i]); }

<code>i = 0; i < strlen(inputWord); ++i</code>

What is an input/output (I/O) stream?

A sequence of bytes that flow from a source to a destination

Assume that each class implements the Comparable interface and creates its own compareTo method. Which one of the following is possible?

Comparable c = new Staff();

If you want to define an alternate ordering, what interface should you implement?

Comparator

Every Java class (other than the Object class) inherits from the Object class.

True

In a variable length parameter list, the actual parameters are automatically put into an array with the variable name specified in the formal parameter.

True

The following diagram demonstrates an is-a relationship Vehicle ^ | Car

True

The values held in an array are called array elements

True

When a class implements the Comparable interface, the compareTo method defines the natural ordering for objects of the class.

True

What type of testing is concerned with testing one component at a time (e.g. testing a class and its methods)?

Unit testing

A programmer must write a 500 line program. Which is most likely the best approach?

Write 10-20 lines, run and debug, write 10-20 more lines, run and debug, repeat

What is unique about an abstract method is that ___________.

a class containing an abstract method must be abstract an abstract method has no body a class containing an abstract method cannot be instantiated

Assume that obj1 and obj2 are instances of a class that has implemented the Comparable interface, and imagine that obj1 is greater than obj2. What will the following method call return? obj1.compareTo(obj2)

a positive value

A(n) ______________ is a step-by-step process for solving a problem.

algorithm

What are the benefits of inheritance?

avoiding redundancy code reuse, testing, maintainability > all of the above

A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative integers are replaced by zeros. The function _____.

can modify the array's elements directly because arrays are passed by pointer

The Comparable interface includes with of the following abstract methods?

compareTo

Person {abstract} | |-------------------------------------| Patient Staff |----------|---------------| | ICUPatient MaternityPatient Doctor After the following two statements are executed, which of the expressions below will return true? Select all that apply. Doctor d = new Doctor(); Patient p = new Patient();

d instanceof Doctor d instanceof Person d instanceof Staff

Regarding a step-by-step process for solving a problem, breaking a "step" into smaller steps is called ____________.

decomposition

In Java, what reserved word is used to establish an inheritance relationship?

extends

Suppose that you want to restrict inheritance for class A (i.e. class A can have no subclasses). What Java reserved word would you use?

final

Indicate what is printed if boolean aBoolean = list[0] == null; is inserted public static void main(String[] args) { Object[] list = new Object[3]; list[0] = null; list[1] = new Object; list[2] = "a string value"; try { INSERT CODE HERE } catch(ArrayIndexOutOfBoundsException e) { System.out.print("ArrayIndexOutOfBoundsException, "); } catch(NullPointerException e) { System.out.print("NullPointerException, "); } catch(Exception e) { System.out.print("Exception, "); } finally { System.out.print("finally, "); } System.out.print("end, "); }

finally, end

Given two integer arrays prevValues and newValues. Which code copies the array prevValues into newValues? The size of both arrays is NUM_ELEMENTS.

for (i = 0; i < NUM_ELEMENTS; ++i) { newVals[i] = prevVals[i];}

Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i;string str;scanf("%s", str);for (YYY) {printf("%c", str[i]);}

i = str.length() - 1; i >= 0; --i

The program should determine the largest integer seen. What should XXX be, if the input values are any integers (negative and non-negative)? All variables are integers and have unknown initial values. for (i = 0; i < 10; ++i) {scanf("%d", &currValue);if (i == 0) {// First iterationXXX;}else if (currValue > maxSoFar) {maxSoFar = currValue;}}

maxSoFar = currValue

How many times will the loop iterate, if the input is 105 107 99 103? scanf("%d", &x);while (x > 100) {// Do somethingscanf("%d", &x);}

2

How many x's will be output? Assume row and col are integers. for (row = 0; row < 2; ++row) {printf("x");for (col = 0; col < 3; ++col) {// Do something}}

2

What is output, if the input is 3 2 4 5? All variables are integers. scanf("%d", &num);for (i = 0; i < num; ++i) {scanf("%d", &curr);printf("%d", curr);}

245

The difference between a checked and unchecked exception is that

- a checked exception must be caught or listed in the throws clause of any method that may throw or propagate it - an unchecked exception inherits from RuntimeException and its descendants

What are the possible ways an exception can be handled in Java?

- ignore it - handle it when it occurs - handle it in another place in the program

Static methods and instance methods are different in that _______

- static methods can access static fields, but not instance fields - instance methods can access instance fields and static fields

What is the output? int n;for (n = 0; n < 10; n = n + 3) {printf("%d ", n);}

0 3 6 9

What is the output? j and k are ints. for (j = 0; j < 2; ++j) {for (k = 0; k < 4; ++k) {if (k == 2) {break;}printf("%d%d ", j, k);}}

00 01 10 11

How many times does the while loop execute for the given input values of -1 4 0 9? userNum = 3;while (userNum > 0) {// Do something// Get userNum from input}

1

What is the output, if n is 3? for (int i = 1; i <= n; ++i) {int factorial = 1;factorial = factorial * i;printf("%d ", factorial);}

1 2 2003

What is the natural ordering of the array below? Integer[] integerArray = {5, 3, 4, 2, 1};

1 2 3 4 5

What is the ending value of sum, if the input is 2 5 7 3? All variables are integers. scanf("%d", &x);sum = 0;for (i = 0; i < x; ++i) {scanf("%d", &currValue);sum += currValue;}

12

What is the output? int x = 18;while (x > 0) {// Output x and a spacex = x / 3; }

18 6 2

For the given program, how many printf statements will execute? void PrintShippingCharge(double itemWeight) {if ((itemWeight > 0.0) && (itemWeight <= 10.0)) {printf("%lf\n", itemWeight * 0.75);} else if ((itemWeight > 10.0) && (itemWeight <= 15.0)) {printf("%lf\n", itemWeight * 0.85);} else if ((itemWeight > 15.0) && (itemWeight <= 20.0)) {printf("%lf\n", itemWeight * 0.95);}}int main(void) {PrintShippingCharge(18);PrintShippingCharge(6);PrintShippingCharge(25);return 0;}

2

What does the underlined portion of the code below represent? public static void main ___(String[]__args)__

An array of String objects which are command line arguments

Suppose a variable has been declared with the protected modifier. Which classes can access the variable?

Any class within the same package and all subclasses

boolean aBoolean = list[3].equals("a string value");

ArrayIndexOutOfBoundsException, finally, end

What is not a key advantage of using an enum as below have versus using a string for myCar and string literals like "red", "green", and "black"? enum CarColor {RED, GREEN, BLACK};CarColor myCar;myCar = GREEN;if (myCar == GREEN) {cout << "Green is our most popular color";}

Capital letters can be used to make the colors clearer

For an enumerated type like below, the compiler assigns what kind of value to RED, GREEN, and BLACK. enum CarColor {RED, GREEN, BLACK};

Integer

Given that integer array x has elements 5, 10, 15, 20, what is the output? int i;for (i = 0; i < 4; ++i) {printf("%d ", x[i] + x[i + 1]); }

Error: Invalid access

What is the output? int main() {for (int i = 0; i < 3; ++i) {printf("%d", i);}printf("%d", i);return 0;}

Error: The variable i is not declared in the right scope

What is the output? int i;for (i = 0; i < 4; ++i) {int factorial = i + 1;for (int j = 0; j < 2; ++j) {printf("%d", factorial * j);}}printf("%d\n", factorial);

Error: factorial is out of scope

A double array is a container object that holds a variable number of values of type double

False

Debugging and testing are the same activity

False

Exceptions in code are indicative of normal program behavior and should be used in normal execution flow.

False

Java supports multiple inheritance.

False

The size of an array object can be changed only within the method in which it was declared

False

When an interface contains abstract methods, the methods are considered private

False

What is the ending value of myStr? char myStr[5] = "Hi"; strcpy(myStr, "Hey");

Hey

Which XXX / YYY declare an array having MAX_SIZE elements and initializes all elements with -1? const int MAX_SIZE = 4;int myNumbers[XXX];int i;for (i = 0; i < YYY; ++i) {myNumbers[i] = -1;}

MAX_SIZE / MAX_SIZE

Which input for char c causes "Done" to be output next? c = 'y';while (c = 'y') {// Do somethingprintf("Enter y to continue, n to quit: \n");scanf("%c", &c);}printf("Done\n");

No such value (infinite loop)

boolean aBoolean = list[0].equals(null);

NullPointerException, finally, end

What term describes having two or more methods in a class with the same name but different signatures?

Overloading

What is the output? void PrintWaterTemperatureForCoffee(int temp) {if (temp < 195) {printf("Too cold.");} else if ((temp >= 195) && (temp <= 205)) {printf("Perfect temperature.");} else if (temp > 205) {printf("Too hot.");}}int main(void) {PrintWaterTemperatureForCoffee(205);PrintWaterTemperatureForCoffee(190);return 0;}

Perfect temperature.Too cold.

Person {abstract} | |-------------------------------------| Patient Staff |----------|---------------| | ICUPatient MaternityPatient Doctor Which one of the following is using polymorphism correctly?

Person p = new ICUPatient();

Which of the standard I/O streams is used to print to the console (e.g. Run I/O tab in jGrasp)?

System.out

Person {abstract} | |-------------------------------------| Patient Staff |----------|---------------| | ICUPatient MaternityPatient Doctor Staff s = new Doctor(); System.out.print(s.toString()); Assume that each class in the diagram above has its own toString method. What will occur after the following lines are executed?

The Doctor class toString method is called.

An abstract method in an interface includes _____.

The method header but no body

Answer Pass, Fail, or Neither to each JUnit test. Assume the necessary imports are made. public class M1QuizClassTest { @Test public void test1() { int one = 1; int two = 2; Assert.assertEquals("Error in test1.", one, two); } @Test public void test2() { double one = 1; double onePointTwo = 1.2; Assert.assertEquals(one, onePointTwo, .3); } @Test public void test3() { double one = 1; double two = 2; Assert.assertEquals(one, two, 1); } @Test public void test4() { String one = "one"; String oneWithSpace = "one"; Assert.assertEquals(one, oneWithSpace); } @Test public void test2() { Object a = new Object(); Object b = new Object(); Assert.assertEquals(a == b); } }

The result of test1? Fail

Answer Pass, Fail, or Neither to each JUnit test. Assume the necessary imports are made. public class M1QuizClassTest { @Test public void test1() { int one = 1; int two = 2; Assert.assertEquals("Error in test1.", one, two); } @Test public void test2() { double one = 1; double onePointTwo = 1.2; Assert.assertEquals(one, onePointTwo, .3); } @Test public void test3() { double one = 1; double two = 2; Assert.assertEquals(one, two, 1); } @Test public void test4() { String one = "one"; String oneWithSpace = "one"; Assert.assertEquals(one, oneWithSpace); } @Test public void test2() { Object a = new Object(); Object b = new Object(); Assert.assertEquals(a == b); } }

The result of test2? Pass

Answer Pass, Fail, or Neither to each JUnit test. Assume the necessary imports are made. public class M1QuizClassTest { @Test public void test1() { int one = 1; int two = 2; Assert.assertEquals("Error in test1.", one, two); } @Test public void test2() { double one = 1; double onePointTwo = 1.2; Assert.assertEquals(one, onePointTwo, .3); } @Test public void test3() { double one = 1; double two = 2; Assert.assertEquals(one, two, 1); } @Test public void test4() { String one = "one"; String oneWithSpace = "one"; Assert.assertEquals(one, oneWithSpace); } @Test public void test2() { Object a = new Object(); Object b = new Object(); Assert.assertEquals(a == b); } }

The result of test3? Pass

Answer Pass, Fail, or Neither to each JUnit test. Assume the necessary imports are made. public class M1QuizClassTest { @Test public void test1() { int one = 1; int two = 2; Assert.assertEquals("Error in test1.", one, two); } @Test public void test2() { double one = 1; double onePointTwo = 1.2; Assert.assertEquals(one, onePointTwo, .3); } @Test public void test3() { double one = 1; double two = 2; Assert.assertEquals(one, two, 1); } @Test public void test4() { String one = "one"; String oneWithSpace = "one"; Assert.assertEquals(one, oneWithSpace); } @Test public void test2() { Object a = new Object(); Object b = new Object(); Assert.assertEquals(a == b); } }

The result of test4? Fail

Answer Pass, Fail, or Neither to each JUnit test. Assume the necessary imports are made. public class M1QuizClassTest { @Test public void test1() { int one = 1; int two = 2; Assert.assertEquals("Error in test1.", one, two); } @Test public void test2() { double one = 1; double onePointTwo = 1.2; Assert.assertEquals(one, onePointTwo, .3); } @Test public void test3() { double one = 1; double two = 2; Assert.assertEquals(one, two, 1); } @Test public void test4() { String one = "one"; String oneWithSpace = "one"; Assert.assertEquals(one, oneWithSpace); } @Test public void test2() { Object a = new Object(); Object b = new Object(); Assert.assertEquals(a == b); } }

The result of test5? Pass

What is the result of running the code below? int [] scores = new int[10]; for (int i = 0; i < scores.length; i++){ if (i %2 == 0) { scores[i] = scores[i] + 1; } }

increases each number at an even index by one

Which one of the choices below initializes an array?

int[] units = {147, 323, 89, 933};

Which best describes the meaning of a 1 (true) being output? Assume v is a large array of ints. int i;bool s;for (i = 0; i < N_SIZE; ++i) {if (v[i] < 0) {s = true; }else {s = false;}}printf("%d", s);

last value is negative

Person {abstract} | |-------------------------------------| Patient Staff |----------|---------------| | ICUPatient MaternityPatient Doctor Assume that only the Doctor class has a method called performSurgery(). Will the code below compile? Staff s = new Doctor(); s.performSurgery();

no

What is the value of tObjects[0] from the array below? Triangle[] tObjects = new Triangle[10];

null

Imagine there is an array called numbers. Which of the expressions below evaluates to the number of positions in the array?

numbers.length

What is the result of running the code below? int [] scores = new int[5]; for (int i = 0; i <= scores.length; i++) { System.out.println(scores[i]); }

prints five zeros then a runtime error occurs

In a class, a support method should be declared as

private

In a class, a service method should be declared as

public

What is the output? char letter1;char letter2;letter1 = 'p';while (letter1 <= 'q') {letter2 = 'x';while (letter2 <= 'y') {printf("%c%c ", letter1, letter2);++letter2;}++letter1;}

px py qx qy

A method's signature does not include which of the following?

return type

System.out.print("start, "); throw new Exception();

start, Exception, finally, end

Static variables and instance variables are different in that

static variables have the same value across every instance

Given two arrays, studentNames that maintains a list of students, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX / YYY. char studentNames[NUM_STUDENTS]; int studentScores[NUM_STUDENTS]; int i; for (i = 0; i < NUM_STUDENTS; ++i) {if (XXX > 80) {printf("%s \n", YYY ); }}

studentScores[i] / studentNames[i]

A loop should sum all inputs, stopping when the input is 0. If the input is 2 4 6 0, sum should end with 12. What should XXX, YYY, and ZZZ be? Choices are in the form XXX / YYY / ZZZ. int sum;int currVal;XXX;scanf("%d", &currVal);while (YYY) {ZZZ;scanf("%d", &currVal);}

sum = 0 / currVal != 0 / sum = sum + currVal

For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY. val = Get next inputXXXWhile val is not -1YYYval = Get next inputPut sum to output

sum = 0 / sum = sum + val

To call a parent method that has been overridden, what Java reserved word should you use?

super

Select the statement XXX that will correctly swap the value in smallArray[0] with smallArray[2]. int smallArray[] = {5, 12, 16};XXX

temp = smallArray[0];smallArray[0] = smallArray[2];smallArray[2] = temp;

When a class implements an interface with an abstract method, ________.

the class must include the header and body of the method.

What Java reserved word allows an object to refer to itself within a method?

this

An exception can be explicitly thrown in a method by using the reserved word along with the specific Exception object to be thrown

throw

Based on the function names, which function most likely should allow the inputVals array parameter to be modified? (Not all parameters are shown.)

void SortDescending (int inputVals[], ...)

Which XXX and YYY will loop as long as the input is an integer less than 100? Choices are in the form XXX / YYY. scanf("%d", &w);while (XXX) {// Do somethingYYY;}

w < 100 / scanf("%d", &w)

Which of the following is/are true regarding the concepts of overloading and overriding?

when overriding, methods have the same signature when overloading, methods have a different signature overriding involves an inherited method and one in the derived class

Person {abstract} | |-------------------------------------| Patient Staff |----------|---------------| | ICUPatient MaternityPatient Doctor The code below is an example of what kind of conversion? Staff s = new Doctor();

widening

What is the output? int columns;int rows;for (rows = 0; rows < 2; ++rows) {for (columns = 0; columns < 3; ++columns) {printf("x");}printf(" ");}

xxx xxx


Ensembles d'études connexes

Unit 2, Assign. 2: Skills and Aptitude

View Set

Fund exam 1:Infection Prevention and Control

View Set

QUIZLET Ounces Equivalents___How Many Ounces in a...

View Set

Intermediate Accounting 2 - Chapter 15 Multiple Choice

View Set

Accounting True and False Chapters 1-8

View Set

EXAM 1: Pediatrics Practice Questions(done)

View Set

Western Civilizations Baker Grove City

View Set