AP Computer Science A : Programming Constructs

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

← Previous 1 3 4 5 6 7 8

Example Question #1 :Program Implementation

Which of the following code samples adequately uses constants?

Possible Answers:

final int rows = 4;

for(int i = 0,l = rows; i < l; i++) {

rows += 2;

}

final int rows;

int intVal;

// Variable intVal read in during this code.... Excerpted...

rows = intVal;

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

System.out.println(i);

}

for(final int i = 0, rows = 5; i < rows; i++) {

System.out.println(i);

}

final String s = "Hello, "

int intVal;

// Variable intVal read in during this code.... Excerpted...

if(intVal < 0) {

s += "World!";

} else if(intVal == 0) {

s += "People!";

} else {

s += "Folks!";

}

const int rows = 4;

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

System.out.println(i);

}

Correct answer:

final int rows;

int intVal;

// Variable intVal read in during this code.... Excerpted...

rows = intVal;

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

System.out.println(i);

}

Explanation:

Remember that a constant variable可以notbe changed once it has been assigned. Normally, you assign the variable immediately on the same line as the declaration. However, if you were to create the constant and wait to assign a value, that would be find syntactically as well. Thus, the correct answer does not have a problem, though it might appear so at first if you did not know this. Now, remember that the way to declare a constant is to use the keyword "final". (The keyword "const" works in some other languages like C++.) All of the incorrect answers (other than the one using "const") alter the constant after it has been defined.

Example Question #1 :Program Implementation

In the following block of code, which of the following lines contains an error?

final int i = 20, j, k;

int l,m=50,n = 2;

j = n + m;

l = m * i + j;

for (int = 0;< m;一个++) {

l += l;

}

m = 20 + j + n * i + m;

j = m + i;

k = 50 + 20 * j;

Possible Answers:

j = m + i;

l = m * i + j;

k = 50 + 20 * j;

l += l;

for (int = 0;< m;一个++)

Correct answer:

j = m + i;

Explanation:

The line

j = m + i;

有一个错误,因为它包含一个重置手术一个constant (final) variable. You are permitted to assign a constant on a line that is not the line of declaration. However, once you do this, you cannot reassign a value. The variablejwas assigned a value on the line:

j = n + m;

Thus, the line above (j = m + i;) represents are-assignment, hence causing an error.

Example Question #2 :Program Implementation

// ^ is Exclusive-Or (XOR) operator

x = x ^ y

y = x ^ y

x = x ^ y

What is the effect of the above snippet of code?

Possible Answers:

The bits ofx一个ndy一个re negated.

The values are the same.

None of the other answers is true.

xis equal toy

The values are switched.

Correct answer:

The values are switched.

Explanation:

Consider two variables:(binary 1010) and(binary 0101). The exclusive or operator returns 1 in every bit that is different in the two numbers and returns 0 in every bit that is the same.

In order to switch the numbers, we must do this:

^

^

^

Continuing with the example,^would return 1111, because no bits are the same between the two numbers. Store this in the variable. Then 1111 ^ 0101 would return 1010. That result will be stored in. Finally, find the value of(1111) ^(1010) 0101,并将结果存储在变化一个ble. The values are now switched.

Example Question #1 :Variable Declarations

Which of the following declares a String array of sizei? (Presume Java syntax.)

Possible Answers:

String s = new String[i];

String[] s = new String[i];

String[] s[i];

String s[] = new String[i];

None of the others

Correct answer:

String[] s = new String[i];

Explanation:

For arrays, it is best to think about the array itself as being a type. So, just as an int namedxis declared:

int x;

And a Stringnamedsis declared:

String s;

哟u begin the declaration of a String array like this:

String[]

This is like saying, "What is coming next will be an array of String objects."

Thus, your best declaration is:

String[] s = new String[i];

This declares the new size as well, appropriately using thenewoperator.

Example Question #11 :Program Implementation

Which of the following code snippets declares an array of integers using an array literal?

Possible Answers:

int[] a = {0:42,1:26,2:134,3:-13,4:45,5:234};

int a = {42,26,134,-13,45,234};

int[] a;

一个[0] = 42;

一个[1] = 26;

一个[2] = 134;

一个[3] = -13;

一个[4] = 45;

一个[5] = 234;

int[] a = new int[6];

一个[0] = 42;

一个[1] = 26;

一个[2] = 134;

一个[3] = -13;

一个[4] = 45;

一个[5] = 234;

int[] a = {42,26,134,-13,45,234};

Correct answer:

int[] a = {42,26,134,-13,45,234};

Explanation:

There are two things to pay attention to for this question. First, you可以make a simple array literal in Java by enclosing the list of array elements in a pair of curly braces. Thus, the array literal

{42,26,134,-13,45,234}

is just fine! However, you do not give the indices. These are inferred. (The first element is at 0, the second at 1, etc.)

Now, you just have to assign this to an一个rrayobject. Thus the following is wrong:

int a = {42,26,134,-13,45,234};

哟u need to have the [] to indicate that一个is an array:

int[] a = {42,26,134,-13,45,234};

Example Question #1 :Programming Constructs

What is the difference between double and int?

Possible Answers:

Int can hold all values whereas double can only store two-digit values.

There is no difference.

Int can hold positive numbers only and double can hold all numbers.

Int can only hold integer values, double can hold any number including decimals.

Int can hold real numbers and double can only hold imaginary numbers.

Correct answer:

Int can only hold integer values, double can hold any number including decimals.

Explanation:

Int can only store integer values, whereas double can hold both integers and decimals, but at the same time uses more memory.

Example Question #2 :Variable Declarations

int x=2;

double y=2.1;

float z=3.0;

int c=(x*y) + z;

What is the value of c

Possible Answers:

6.3

7.2

6

7

7.1

Correct answer:

7

Explanation:

Remember that if a variable is declared as an integer, it can't have any decimals.

int c=(x*y) + z;

int c=(2*2.1)+3.0

From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.

int c=4.2+3

int c= 4+3

int c = 7

Any leftover fractions are cut off, so the answer is shortened to just 7.

Example Question #1 :Variable Declarations

#include

using namespace std;

int main()
{
string str("ComputerScience");
string mystr;
str = str + "SampleQuestionCS";
mystr = str.substr(7,8);

return 0;
}

What is the value of mystr after this code is run?

Possible Answers:

QuestionCS

SampleQuestion

Science

rScience

ComputerS

Correct answer:

rScience

Explanation:

Two strings are created here: str and mystr.

In the 3rd line of main, str is concatenated with another string, making it even longer.

In the fourth line, the substr function is called. This function takes in 2 inputs, the starting index and the length of the string.

So, starting from index 7 of str, we get "r". Now, grabbing a total of 8 characters, including the "r", we get "rScience".

mystr="rScience".

Example Question #1 :Variable Declarations

  • Declare a string set to the the word cat.
  • Declare a list of strings with the words hello, mama, and meow in it.
  • Declare a hashmap of pairs with the word world having a value of earth.
Possible Answers:

String cat = "cat";

List stringList = new ArrayList();

stringList.add("hello");

stringList.add("mama");

stringList.add("meow");

HashMap hashStrings = new HashMap();

hashStrings.put("world", "earth");

String cat;

List stringList;

HashMap hashStrings;

String cat = "cat";

List stringList = new ArrayList();

HashMap hashStrings = new HashMap();

hashStrings.put("world", "earth");

List stringList = new ArrayList();

stringList.add("hello");

stringList.add("mama");

stringList.add("meow");

HashMap hashStrings = new HashMap();

hashStrings.put("world", "earth");

Correct answer:

String cat = "cat";

List stringList = new ArrayList();

stringList.add("hello");

stringList.add("mama");

stringList.add("meow");

HashMap hashStrings = new HashMap();

hashStrings.put("world", "earth");

Explanation:

The correct answer defines all of the variables specified in the prompt. The answer with just the variable definitons is incorrect because it does not specify the declarations in the prompt. One the answers does not have the first variable defined so it is incorrect. And the final answer does not have the List variable declared, so it is also incorrect.

Example Question #1 :Variable Declarations

Which of the following declares a String array of sizei? (Presume Java syntax.)

Possible Answers:

String s = new String[i];

String[] s = new String[i];

String[] s[i];

String s[] = new String[i];

None of the others

Correct answer:

String[] s = new String[i];

Explanation:

For arrays, it is best to think about the array itself as being a type. So, just as an int namedxis declared:

int x;

And a Stringnamedsis declared:

String s;

哟u begin the declaration of a String array like this:

String[]

This is like saying, "What is coming next will be an array of String objects."

Thus, your best declaration is:

String[] s = new String[i];

This declares the new size as well, appropriately using thenewoperator.

← Previous 1 3 4 5 6 7 8
Learning Tools by Varsity Tutors