AP Computer Science A › Variable Declarations
String cat = "cat";
List<String> stringList = new ArrayList<String>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
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>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
HashMap<String, String> hashStrings = new HashMap<String, String>();
hashStrings.put("world", "earth");
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.
String cat = "cat";
List<String> stringList = new ArrayList<String>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
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>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
HashMap<String, String> hashStrings = new HashMap<String, String>();
hashStrings.put("world", "earth");
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.
Which of these produce a typecasting error?
float f = double d;
double d = int i;
int i = byte b;
long l = short s;
float f = double d;
double d = int i;
int i = byte b;
long l = short s;
None of the answers are wrong.
These problems all involve implicit type casting. For there to be an issue, you'd have to be converting a primitive that takes up more issue into one that requires less memory. The only one that fits that prerequisite is converting a double into a float. The primitive data chain is:
byte < short < int < long < float < double
// ^ is Exclusive-Or (XOR) operator
x = x ^ y
y = x ^ y
x = x ^ y
What is the effect of the above snippet of code?
The values are switched.
x
is equal to y
The bits of x
and y
are negated.
The values are the same.
None of the other answers is true.
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) which is 0101, and store the result in variable
. The values are now switched.
What are the 3 different manners (in terms of accessibilitiy) in which you can declare a variable and what are the differences between the 3?
Public: accessible to everyone
Private: accessible to members of the class
Protected:accessible to classes in the same package and subclasses
Public: accessible to everyone
Private: accessible to only members of the method
Protected: accessible to subclass
Public: accessible to that sole class
Private: accessible to the method or instance in which it was called
Protected: acts as the default setting
Public is accessible to everyone regardless if the two classes are in the same package or not.
Protected: Only allows access of those extended are packaging.
Private: Its meant to keep private in order for only members of that specific class to see it.
String cat = "cat";
List<String> stringList = new ArrayList<String>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
HashMap<String, String> hashStrings = new HashMap<String, String>();
hashStrings.put("world", "earth");
String cat = "cat";
List<String> stringList = new ArrayList<String>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
HashMap<String, String> hashStrings = new HashMap<String, String>();
hashStrings.put("world", "earth");
String cat;
List<String> stringList;
HashMap<String, String> hashStrings;
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");
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>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
HashMap<String, String> hashStrings = new HashMap<String, String>();
hashStrings.put("world", "earth");
List<String> stringList = new ArrayList<String>();
stringList.add("hello");
stringList.add("mama");
stringList.add("meow");
HashMap<String, String> hashStrings = new HashMap<String, String>();
hashStrings.put("world", "earth");
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.
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
7
7
7.2
7.2
7.1
7.1
6.3
6.3
6
6
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.
#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?
rScience
rScience
Science
Science
ComputerS
ComputerS
SampleQuestion
SampleQuestion
QuestionCS
QuestionCS
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".
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
7
7.2
7.1
6.3
6
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.
// ^ is Exclusive-Or (XOR) operator
x = x ^ y
y = x ^ y
x = x ^ y
What is the effect of the above snippet of code?
The values are switched.
x
is equal to y
The bits of x
and y
are negated.
The values are the same.
None of the other answers is true.
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) which is 0101, and store the result in variable
. The values are now switched.