Computer Science : Computer Science

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #3 : Comparing Run Times

True or False.

The code snippet A has a more efficient running time than code snippet B.

 

(A)

for (int i = 0; i < 25; i++) {

for (int j = i; j < 25; j++) {

 

}

}

 

(B)

for (int i = 0; i < 25; i++ {

for (int j = 0; j < 25; j++) {

 

}

}

Possible Answers:

False

True

Correct answer:

False

Explanation:

Code snippet A has a running time of O(N2). Code snippet B has a running time of O(N). While the two code snippets may look the same, the second for loop in code snippet A sets j=i. Since j is relying on i, it's multiplying the first for loop's running time by the second for loop's running time. This gives us O(N*N) or just O(N2). 

Example Question #271 : Computer Science

for( int i = 0; i < n; ++i){

    for( int j = 1; j < n; j *= 2){

        someFunction();

    }

}

 

For the code above, what is the run time in Big O notation?

Possible Answers:

O( log(n) )

None of the above

O(  )

O(n log(n) )

O( n )

Correct answer:

O(n log(n) )

Explanation:

At first glance we might be tempted to pick O(  ) because there are 2 for loops. But, upon closer inspection we can see that the first loop will yield a O( n ) running time but the second loop does not. The second loop has only an O( log(n) ) running time because "j" doubles each iteration and does not increase linearly. That will yield O( log(n) ) since O( log(n) ) is a much faster running time. So the final result is O( n log(n) ).

Example Question #272 : Computer Science

BITWISE XOR OPERATION

Given the following binary values

a = 0100 1110

b = 0011 0101

perform an XOR operation (c = a^b). What is the result?

Possible Answers:

c = 0111 1011

c = 0000 0100

c = 0111 1111

c = 0111 0011

Correct answer:

c = 0111 1011

Explanation:

Performing a bitwise excludive OR constitutes in taking both binary values and evaluating as follows: either one or the other (1) never both (0). This is the truth table for a bitwise XOR operation:

 Blank flowchart   new page  2

Taking both a and b and performing the operation bit by bit we get the following result:

 Result

Example Question #273 : Computer Science

True or False.

 

There is a runtime exception in this code snippet. 

 

int wait_time = 0;

int wait_time = 5;

for (int i = 0; i < wait_time; i++) {

System.out.println(wait_time);

}

Possible Answers:

False

True

Correct answer:

True

Explanation:

Yes, there is a runtime exception in the code snippet. The int wait_time is defined twice which will give a runtime exception. This can be fixed by not declaring int before the second assignment of the variable wait_time. 

Example Question #1 : Pre Conditions

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?

Possible Answers:

The arguments in total should be flipped. Such as: total = sum(9,6);

The code is missing the math library. It should have

#include

The function prototype is missing.

There is nothing wrong with this code.

Correct answer:

The function prototype is missing.

Explanation:

Taking a look at each answer, we can see that the order of the arguments passed in the sub function is correct because of the fact that num1 is being subtracted from num2. Also, for simple math operations there isn't a need to include the math library. Lastly, any operation can be done within a function or in main; there is no wrong method however, creating and using functions makes the code in main() easier to read and cleaner.

Now within main(), when the sub() function is called the function has not been defined; meaning that it is not within the scope. This means that we should place the function prototype before main() so that when the function is called, the compiler know that there's a function called sub() that takes in two integer parameters and returns an integer. Therefore this addition will fix our scope issue.

 

#include <iostream>

using namespace std;

int sub(int num1, int num2); //This addition is the function prototype

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

}

 

Note: Another fix to this issue, is placing the functions right before main(). Although this method is not particularly recommended because it makes main() harder to find, people still use it. The following is an example of fixing the issue by placing the function before main(). Note that the function prototype is not needed when using this method:

 

#include <iostream>

using namespace std;

 

int sub(int num1, int num2) {

return (num2 - num1);

}

 

int main() {

int total = 0;

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

total = sub(6,9);

cout << total;

}

 

Example Question #273 : Computer Science

double square(double n){

return n*n;

}

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

Possible Answers:

The result will be a positive number.

The result will be stored in a new variable.

It is impossible to tell.

The result will be a negative number.

The value of the input parameter changes.

Correct answer:

The result will be a positive number.

Explanation:

Squaring a real number will always produce a positive number. The result does not have to be stored in a new variable; it could be a value that is only needed for a one-off expression, thus, not worthy to be stored in memory. Lastly, since the input was passed by value and not by reference, its initial value will stay the same.

Example Question #1 : Assertions

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

Possible Answers:
int num = 10;
num = (num > 0) ? 11: 12;
System.out.println(num);
int num = 5;
num = (num < 0) ? 10: 11;
System.out.println(num);

None of the answers are correct.

int num = 5;
num = (num > 0) ? 10: 11;
System.out.println(num);
int num = 5;
num = (num > 0) ? 11: 10;
System.out.println(num);
Correct answer:
int num = 5;
num = (num > 0) ? 10: 11;
System.out.println(num);
Explanation:

Each bit of code has something similar to this:

num = (boolean statement) ? X : Y;

The bit at the end with the ? and : is called a ternary operator. A ternary operator is a way of condensing an if-else statement. A ternary operator works like this:

<boolean statement> ? <do this if true> : <do this if false>

The correct answer is

int num = 5;
num = (num > 0) ? 10: 11;
System.out.println(num);

Therefore, the ternary operator portion of code, when converted to an if-else, looks like this:

if (num > 0) {
num = 10;
else {
num = 11;
}
 
Because num is 5, which is greater than 0, it would go into the if, so num would then get 10. Then, num gets printed, which means 10 gets printed (the correct answer).

Example Question #42 : Program Analysis

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;

}

}

Possible Answers:

True

False

Correct answer:

True

Explanation:

The assertion is used to validate user input. The user is able to input anything into the value at args[0]. Since this is the case, we must validate and make sure that the user is inputting what we want. To make this code snippet better, add error checking to do something if the user did not input an integer. 

Example Question #1 : Object Oriented Program Design

Which of the following lines represents a data member?

1. class animal
2. {
3. public:
4. animal();
5. void fetch();
6. private:
7. char bone;
8. }

Possible Answers:

Line 6

Line 1

Line 7

Line 3

Line 5

Correct answer:

Line 7

Explanation:

Data members are variables created in a class, and are typically found under the private: label, as data members are typically kept local to that class. It can be distinguished from member functions because of its lack of parenthesis().

Example Question #2 : Object Oriented Program Design

Which of the following operations allow you to define the function func1() outside of the class definition?

1. class school
2. {
3. public:
4. school();
5. void func1();
6. private:
7. char class;
8. }

Possible Answers:

=

,

->

::

;

Correct answer:

::

Explanation:

The function template for func1() has been defined in the class definition, however the implementation of the function has not. In order to specify what the function does, you must say

classname::functionname()

{

}

Or, in our case,

school::function1()

{

//function definition goes in here

}

Learning Tools by Varsity Tutors