AP COMPUTER SCIENCE A UNIT 1 EXAM
What is (6 % 2) * 7?
0
What is output by the following code? int val = -2; val++; val--; val++; val++; val++; val--; val++; System.out.println(val);
1
Consider the following code: int c = 3 - 37 % 2; System.out.println(c); What is output?
2
What is the value of w after executing this segment of code? int w = 18; w += 18;
36
What is output by the following code? int a = 91; System.out.println(a / 2);
45
Consider the following code: int x = 9; int y = 6; System.out.println( (x*y)/x ); What is output?
6
There are two integer variables in our program, minutes and hours, which represents time. If in the program, we increase the number of minutes by one, which of the following lines of code will correctly update hour and minutes?
hours = hours + minutes / 60; minutes = minutes % 60;
Which of the following would properly create A and B as integer variables?
int A; int B;
Correct the following code so that q stores the nearest integer below 82.3847. int q = 82.3847;
int q = (int) 82.3847;
Consider the following variable declaration: double y = 52; Does a cast need to be added so this code will compile and run successfully? ______. If so, what should be typed for this cast? _______
no, nothing
Which of the following is a legal variable name in Java?
ans
Which of the following data types would be most appropriate to use when recording whether a switch is in the "on" or "off" position?
boolean
For which of the following would modular division be LEAST likely to be useful?
converting decimals to whole numbers
Consider the following code: int x = -3; x--; System.out.println(x); What is output?
-4
The following code is intended to input three integers and print the average. What is a potential problem with the code as written? System.out.println("Please enter three integers: "); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); System.out.println("The average is: " + (a + b + c) / 3);
It needs a cast so that the decimal portion will be shown.
Which of the following is NOT a primitive data type?
String
Which of the following correctly stores the word umbrella in a variable called stuff?
String stuff = "umbrella";
Assuming that scan is a properly initialized Scanner variable, which of the following correctly inputs a String?
String val = scan.nextLine();
Which of the following will print the ones column of an integer stored in x?
System.out.print(x % 10);
When might you encounter a problem with integer overflow?
When trying to store an integer which is too big to be stored in an int variable