Program Correctness

Practice Questions

Computer Science › Program Correctness

Questions
8
1

Which of the following code excerpts would output "10"?

2

Which of the following code excerpts would output "10"?

3

True or False.

The assertion in this code snippet is to prevent users from inputting bad data.

public class UserInput {

int userInput;

public static void main(String[] args) {

assertTrue(isInteger(args[0]));

userInput = args[0];

userInput = 25 - userInput;

}

}

4

True or False.

The assertion in this code snippet is to prevent users from inputting bad data.

public class UserInput {

int userInput;

public static void main(String[] args) {

assertTrue(isInteger(args[0]));

userInput = args[0];

userInput = 25 - userInput;

}

}

5

double square(double n){

return n*n;

}

What MUST be true immediately after the above code snippet has run?

6

double square(double n){

return n*n;

}

What MUST be true immediately after the above code snippet has run?

7

USING FUNCTIONS

Consider the following C++ code to perform the following subtraction:

9 - 6:

**#include <iostream>**

**using namespace std;**

**int main() {**

**int total = 0;**

**cout << "This code performs the following math operation: 9 - 6 = ";**

**total = sub(6,9);**

**cout << total;**

**}**

``

**int sub(int num1, int num2) {**

**return (num2 - num1);**

**}**

Is there anything wrong with the code?

8

USING FUNCTIONS

Consider the following C++ code to perform the following subtraction:

9 - 6:

**#include <iostream>**

**using namespace std;**

**int main() {**

**int total = 0;**

**cout << "This code performs the following math operation: 9 - 6 = ";**

**total = sub(6,9);**

**cout << total;**

**}**

``

**int sub(int num1, int num2) {**

**return (num2 - num1);**

**}**

Is there anything wrong with the code?

Return to subject