Choosing Appropriate Data Structures

Help Questions

AP Computer Science A › Choosing Appropriate Data Structures

Questions 1 - 10
1

USING POINTERS

Study the following pseudocode.

**int * var1;**

**int foo = 63;**

**var1 = &foo;**

**var2 = *var1;**

What are the values of var1 and var2?

var1 is assigned the memory address value of foo.

var2 is assigned a value of 63

var1 is assigned a value of 63

var2 is assigned a value off 63

var1 is assigned a value of 63

var2 is assigned the memory address value of var1

var1 is assigned the memory address value of foo

var2 is assigned the memory address value of var1

Explanation

Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:

int * var1;

Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.

The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:

var1 = &foo;

Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.

Next in the code, we have the following statement:

**var2 = *var1;**

Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.

2

USING POINTERS

Study the following pseudocode.

**int * var1;**

**int foo = 63;**

**var1 = &foo;**

**var2 = *var1;**

What are the values of var1 and var2?

var1 is assigned the memory address value of foo.

var2 is assigned a value of 63

var1 is assigned a value of 63

var2 is assigned a value off 63

var1 is assigned a value of 63

var2 is assigned the memory address value of var1

var1 is assigned the memory address value of foo

var2 is assigned the memory address value of var1

Explanation

Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:

int * var1;

Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.

The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:

var1 = &foo;

Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.

Next in the code, we have the following statement:

**var2 = *var1;**

Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.

3

USING POINTERS

Study the following pseudocode.

**int * var1;**

**int foo = 63;**

**var1 = &foo;**

**var2 = *var1;**

What are the values of var1 and var2?

var1 is assigned the memory address value of foo.

var2 is assigned a value of 63

var1 is assigned a value of 63

var2 is assigned a value off 63

var1 is assigned a value of 63

var2 is assigned the memory address value of var1

var1 is assigned the memory address value of foo

var2 is assigned the memory address value of var1

Explanation

Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:

int * var1;

Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.

The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:

var1 = &foo;

Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.

Next in the code, we have the following statement:

**var2 = *var1;**

Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.

4

What is the difference between declaring a function as void and declaring it as int?

A void declaration does not expect an output to that function, whereas an int function declarations expects an intger value as the output to the function.

There is no difference.

A void function has no input and an int function has at least one integer input.

An int function needs to declare an integer variable inside the function and a void function doesn't.

A void function can have any return type whereas an int function can only have one return type.

Explanation

Function delcarations are very significant, as you must define what the return type of the function is. A void function doesn't need a return type, whereas the int function must have an integer value as its return type.

5

What is the difference between declaring a function as void and declaring it as int?

A void declaration does not expect an output to that function, whereas an int function declarations expects an intger value as the output to the function.

There is no difference.

A void function has no input and an int function has at least one integer input.

An int function needs to declare an integer variable inside the function and a void function doesn't.

A void function can have any return type whereas an int function can only have one return type.

Explanation

Function delcarations are very significant, as you must define what the return type of the function is. A void function doesn't need a return type, whereas the int function must have an integer value as its return type.

6

What is the difference between declaring a function as void and declaring it as int?

A void declaration does not expect an output to that function, whereas an int function declarations expects an intger value as the output to the function.

There is no difference.

A void function has no input and an int function has at least one integer input.

An int function needs to declare an integer variable inside the function and a void function doesn't.

A void function can have any return type whereas an int function can only have one return type.

Explanation

Function delcarations are very significant, as you must define what the return type of the function is. A void function doesn't need a return type, whereas the int function must have an integer value as its return type.

7

What is not a feature of the Java programming language?

Ability to write functions without making them class methods

Garbage Collection

Object Oriented Programming

Compile time error checking

primitive data types such as int, boolean and double

Explanation

In Java, the only way to write functions is to make them class methods. Java does have primitive types int, boolean double. There is compile time error checking. Java is an Object Oriented langauge and supports the OO paradigm. There is automatic garbage collection support, which helps manage memory for the user.

8

CHOOSING LOOPS

Imagine that I want to write a code for a bank that asks a user whether he/she wants to make a withdrawal, make a deposit, or quit. What type of loop would be best to use in order to make sure the user is able to do as many transactions as they want until they press quit to end the program?

A do-while loop.

A while loop.

A for loop.

An infinite loop.

Explanation

In our problem, the program has to run AT LEAST ONCE in order to present all of the options to the user. Because of this reason, the best option is a do-while loop because the statements within that loop execute at least one time and the condition to get out of the loop is at the end. In this case, the exit condition would be if the user presses quit (after the options have been shown at least once).

A while loop might work in this case; however it is not necessarily the smartest option. This is because the condition is tested before anything is executed within the loop. This means that whatever is inside the while loop does not necessarily have to be executed at all.

A for loop is also not a good option for this problem because a for loop is executed a set amount of times. In this case, we don't know if the user is going to want to do 1 transaction or 10. Therefore, a for loop is not the best choice.

An infinite loop is also not what we want because it runs forever. In our case, we want the code to stop prompting the user and end after "quit."

9

CHOOSING LOOPS

Imagine that I want to write a code for a bank that asks a user whether he/she wants to make a withdrawal, make a deposit, or quit. What type of loop would be best to use in order to make sure the user is able to do as many transactions as they want until they press quit to end the program?

A do-while loop.

A while loop.

A for loop.

An infinite loop.

Explanation

In our problem, the program has to run AT LEAST ONCE in order to present all of the options to the user. Because of this reason, the best option is a do-while loop because the statements within that loop execute at least one time and the condition to get out of the loop is at the end. In this case, the exit condition would be if the user presses quit (after the options have been shown at least once).

A while loop might work in this case; however it is not necessarily the smartest option. This is because the condition is tested before anything is executed within the loop. This means that whatever is inside the while loop does not necessarily have to be executed at all.

A for loop is also not a good option for this problem because a for loop is executed a set amount of times. In this case, we don't know if the user is going to want to do 1 transaction or 10. Therefore, a for loop is not the best choice.

An infinite loop is also not what we want because it runs forever. In our case, we want the code to stop prompting the user and end after "quit."

10

CHOOSING LOOPS

Imagine that I want to write a code for a bank that asks a user whether he/she wants to make a withdrawal, make a deposit, or quit. What type of loop would be best to use in order to make sure the user is able to do as many transactions as they want until they press quit to end the program?

A do-while loop.

A while loop.

A for loop.

An infinite loop.

Explanation

In our problem, the program has to run AT LEAST ONCE in order to present all of the options to the user. Because of this reason, the best option is a do-while loop because the statements within that loop execute at least one time and the condition to get out of the loop is at the end. In this case, the exit condition would be if the user presses quit (after the options have been shown at least once).

A while loop might work in this case; however it is not necessarily the smartest option. This is because the condition is tested before anything is executed within the loop. This means that whatever is inside the while loop does not necessarily have to be executed at all.

A for loop is also not a good option for this problem because a for loop is executed a set amount of times. In this case, we don't know if the user is going to want to do 1 transaction or 10. Therefore, a for loop is not the best choice.

An infinite loop is also not what we want because it runs forever. In our case, we want the code to stop prompting the user and end after "quit."

Page 1 of 3
Return to subject