AP Computer Science A- Primitive Types
Which Java Data Type would be best sited to represent the number of days left until the AP Computer Science Exam?
int
Which of the following is a proper way to declare and initialize a variable in Java?
int myNumber = 10;
What is the correct line of code needed to request a whole number from a user? Assume a Scanner object called input has already been created.
int num= input.nextInt ();
Which of the following variable names follows best practices for naming a variable?
numApples
What symbol is not a Java arithmetic operator?
#
Which of the following could be stored in the variable: char initial;
'k' (characters use single quotes)
What will this code segment output? public class Printing { public static void main(String[] args) { System.out.println("*****"); System.out.println("****"); System.out.println("***"); System.out.println("**"); System.out.println("*"); } }
***** **** *** ** *
What will be stored in the variable modulo after running the following code snippet? public static void main(String[] args) { int num = 2; int modulo = 15; modulo %= num; }
1 (15/2= 7 Remainder 1)
What will be stored in the variable age after this code snippet executes? public static void main(String[] args) { int age = 16; age++; age--; age++; }
17
What will be the output of the following code snippet: public class Calculator { public static void main (String[] args) { int first= 7; int second= 2; int result= 7 / 2; System.out.println (result); } }
3 (7/2= 3.5 but since it is an integer the .5 is taken off)
What is the result of this expression: 150 % 100
50 (150/100 has a remainder of 50)
What will this java expression evaluate to? (int) 9.9
9
In Java the = sign is a statement that means...
An assignment of the right hand side value to the left hand side variable.
What will be the output of the following code snippet? public class Casting { public static void main(String[] args) { int total = 100; int numPeople = 40; double average; average = total / (double) numPeople; System.out.println("Average: " + average); } }
Average: 2.5
What will be the output of the following code snippet: public class Variables { public static void main(String[] args) { int totalBirds = 150; double averageTime = 45.7; String mostCommon = "Mallard Duck"; System.out.println("Bird Watching Results"); System.out.print("Total birds seen: "); System.out.println(totalBirds); System.out.print("Average time between sightings: "); System.out.println(averageTime); System.out.print("Most common bird seen was "); System.out.println(mostCommon); } }
Bird Watching Results Total birds seen: 150 Average time between sightings: 45.7 Most common bird seen was Mallard Duck
Which Java Data Type would be the best suited to represent whether or not a student has completed their homework?
Boolean (true or false)
What is casting in Java?
Casting is turning a value of one type into another type.
What will this code segment output? System.out.println ("Hello"); System.out.println ("World");
Hello World
What does the keyword final do?
It prevents variables from being altered.
What is the correct syntax for creating a Scanner object?
Scanner objectName = new Scanner(System.in);
Which of the choices below is not a primitive type in Java?
String (String is a reference type variable and it starts with a capital letter)
What code segment will print "Hello Karel" to the screen in Java?
System.out.println ("Hello Karel");
What will be the output of the following code snippet and why? public static void main(String[] args) { int num = 2; int dividend = 5; dividend /= num; System.out.println(dividend); }
The output will be 2 because when two integers are divided in Java, the decimal portion is always truncated.
Consider the following code snippet: public static void main (String args []){ final int z; z=20; z=30; System.out.println (z); What value of z is actually printed?
This code gives an error because you cannot assign a value to final variable z.
Which Java data type would be best suited to represent the amount of money in a bank account?
double (decimal value)
What is the difference between the int type and the double type?
double can be assigned numbers like 1,3, 3.5, -4, but int can only be assigned numbers like 1, 4, -7, -10.
What is the correct syntax for the main method in Java?
public static void main (String [] args) { }
What code snippet will end with num holding the value 15?
public static void main(String[] args) { int num = 14; num++; num--; num++; } public static void main(String[] args) { int num = 225; num /= 15; System.out.println(num); } public static void main(String[] args) { int num = 31; num %= 16; System.out.println(num); }
What are the memory values associated with the variables x, y, and z after the code snippet below executes? int x = 7; double y = 2.0; boolean z = false; x = x + 3; z = true;
x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true.
Which of the following is true about primitive types in Java?
•a variable is the name given to a memory location •the value stored in a variable can be changed during program execution •all variables must be declared before use.