# AP Computer Science A : Variable Declarations

## Example Questions

### Example Question #1 : Variable Declarations

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

None of the others

String s = new String[i];

String[] s = new String[i];

String[] s[i];

String s[] = new String[i];

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 named x is declared:

int x;

And a String named s is declared:

String s;

You 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."

String[] s = new String[i];

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

### Example Question #1 : Programming Constructs

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

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

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

int[] a = new int[6];

a[0] = 42;

a[1] = 26;

a[2] = 134;

a[3] = -13;

a[4] = 45;

a[5] = 234;

int[] a;

a[0] = 42;

a[1] = 26;

a[2] = 134;

a[3] = -13;

a[4] = 45;

a[5] = 234;

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

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

Explanation:

There are two things to pay attention to for this question.  First, you can 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 array object.  Thus the following is wrong:

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

You need to have the [] to indicate that a is an array:

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

### Example Question #1 : Programming Constructs

What is the difference between double and int?

There is no difference.

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

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

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

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.

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 #1 : Variable Declarations

int x=2;

double y=2.1;

float z=3.0;

int c=(x*y) + z;

What is the value of c

7

6

6.3

7.1

7.2

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 : Program Implementation

#include <iostream>

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?

SampleQuestion

ComputerS

rScience

QuestionCS

Science

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 #11 : Program Implementation

• 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 <string, string> <key, value> pairs with the word world having a value of earth.

String cat = "cat";

List<String> stringList = new ArrayList<String>();

HashMap<String, String> hashStrings = new HashMap<String, String>();

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

String cat;

List<String> stringList;

HashMap<String, String> hashStrings;

String cat = "cat";

List<String> stringList = new ArrayList<String>();

HashMap<String, String> hashStrings = new HashMap<String, String>();

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

List<String> stringList = new ArrayList<String>();

HashMap<String, String> hashStrings = new HashMap<String, String>();

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

String cat = "cat";

List<String> stringList = new ArrayList<String>();