CodeHS Unit 1: Primitive Types (1.1-1.6)
What will this code segment output? System.out.println("Hello"); System.out.println("World"); A. Hello World B. HelloWorld C. Hello World D. Hello World
A. Hello World
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; } A. 1 B. 2 C. 15 D. This code will throw an error because the compound assignment operator is written incorrectly.
A. 1
Which code snippet below will end with num holding the value 15? A. public static void main(String[ ] args) { int num = 14; num++; num--; num++; } B. public static void main(String[ ] args) { int num = 225; num /= 15; System.out.println(num); } C. public static void main(String[ ] args) { int num = 31; num %= 16; System.out.println(num); } D. All of the above
D. All of the above
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); } } A. Average: 0.4 B. Average: 0.0 C. Average: 2.0 D. Average: 2.5
D. Average: 2.5
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. A. String num = input.nextInt(); B. int num = Integer.nextInt(); C. int num = input.nextDouble(); D. int num = input.nextInt();
D. int num = input.nextInt();
Which of the following variable names follows best practices for naming a variable? A. 5apples B. someVariable C. applesnum D. numApples
D. numApples
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? A. 20 B. 30 C. This code gives an error. D. Nothing is printed.
c. This code gives an error.
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); } } A. Bird Watching Results Total birds seen: 150 Average time between sightings: 45.7 Most common bird seen was Mallard Duck B. Bird Watching Results Total birds seen: 150 Average time between sightings: 45.7 Most common bird seen was Mallard Duck C. Bird Watching Results Total birds seen: 150 Average time between sightings: 45.7 Most common bird seen was Mallard Duck D. Bird Watching Results Total birds seen: totalBirds Average time between sightings: averageTime Most common bird seen was
B. Bird Watching Results Total birds seen: 150 Average time between sightings: 45.7 Most common bird seen was Mallard Duck
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++; } A. 16 B. 17 C. 15 D. This code will throw an error because age can only hold one value at a time.
B. 17
What is casting in Java? A. What Karel does when she breaks her leg. B. Casting is turning a value of one type into another type. C. A way to get user input in Java. D. A way to print to the screen in Java.
B. Casting is turning a value of one type into another type.
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; A. x holds the int value 7, y holds the double value 2.0 and z holds the boolean value false. B. x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true. C. This code snippet will result in a compile time error. D. x holds the int value 10, y holds the double value 2.0 and z holds the boolean value false.
B. x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true.
What is the correct syntax for writing the main method in Java? A. public void main( ) { } B. public static void main( ) { } C. public static void main(String[ ] args) { } D. public static void String(main) { }
C. public static void main(String[ ] args) { }
Which of the below is NOT a Java arithmetic operator? A. + B. - C. # D. /
C. #
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 = first / second; System.out.println(result); } } A. 3.5 B. 3.50 C. 3 D. 3 1/2
C. 3
What is the result of this expression? 150 % 100 A. 0 B. 100 C. 50 D. 3
C. 50
What will this java expression evaluate to? (int) 9.9 A. 0 B. 10 C. 9 D. 5
C. 9
What does the keyword final do? A. It's necessary to declare a variable. B. Enables the use of println on a variable. C. It prevents variables from being altered. D. It indicates that the program has finished executing.
C. It prevents variables from being altered.
What is the correct syntax for creating a Scanner object? A. Scanner = new Scanner; B. scanner object = new scanner(System.in); C. Scanner objectName = new Scanner(System.in); D. Scanner objectName = System.in;
C. Scanner objectName = new Scanner(System.in);
Which code segment will print "Hello Karel" to the screen in Java? A. System.out.printLine("Hello Karel"); B. print "Hello Karel"; C. System.out.println("Hello Karel"); D. System.println("Hello Karel");
C. 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); } A. The output will be 2.5 because 5 divided by 2 is always 2.5. B. The output will be 2.0 because when two integers are divided in Java, the decimal portion is always truncated. C. The output will be 2 because when two integers are divided in Java, the decimal portion is always truncated. D. This code will throw an error because the variable dividend cannot store a double type in memory.
C. The output will be 2 because when two integers are divided in Java, the decimal portion is always truncated.
In Java, the = sign in a statement means A. is equal to. B. the left hand side is equal to the right hand side in the equation. C. an assignment of the right hand side value to the left hand side variable. D. that two equivalent relationships can be compared using a Boolean comparison.
C. an assignment of the right hand side value to the left hand side variable.
Which of the following is a proper way to declare and initialize a variable in Java? A. myInteger = 100; B. char = 'a'; C. int myNumber = 10; D. "Variable"
C. int myNumber = 10;
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("*"); } } A. This won't print anything. B. * ** *** **** ***** C. It will only print the first line as ***** since only one print statement can be used. D. ***** **** *** ** *
D. ***** **** *** ** *
