Docsity

AP Computer Science Exam Review: Java Fundamentals, Exams of Computer Science

Toggle sidebar
bg1
AP Computer Science Exam Review
Which of the following is a valid string in Java?
A. "a"
B. "@#$"
C. "hello"
D. "123"
E. All - Answer -E. All
To output:
HelloThere
You would need to use:
System.out._____ ("Hello");System.out.print("There");
A. Print
B. println
C. None of the options shown
D. Println
E. print - Answer -B. println
________ is the process of finding and eliminating errors in code. - Answer -Debugging
What will happen if the computer fails to compile your code? - Answer -An error
message will be displayed
Which of the following would Java interpret as being a String?
A. ```Hello World```
B. <p>Hello World</p>
C. `Hello World`
D. "Hello World"
E. 'Hello World' - Answer -D. "Hello World"
What is the name for the type of data that consists of characters enclosed in double
quotes, such as "Hello world!"? - Answer -String
A collection of characters in quotes is a __________ - Answer -String
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download AP Computer Science Exam Review: Java Fundamentals and more Exams Computer Science in PDF only on Docsity!

AP Computer Science Exam Review

Which of the following is a valid string in Java? A. "a" B. "@#$" C. "hello" D. "123" E. All - Answer -E. All To output: HelloThere You would need to use: System.out._____ ("Hello");System.out.print("There"); A. Print B. println C. None of the options shown D. Println E. print - Answer -B. println ________ is the process of finding and eliminating errors in code. - Answer -Debugging What will happen if the computer fails to compile your code? - Answer -An error message will be displayed Which of the following would Java interpret as being a String? A. Hello World B.

Hello World

C. Hello World D. "Hello World" E. 'Hello World' - Answer -D. "Hello World" What is the name for the type of data that consists of characters enclosed in double quotes, such as "Hello world!"? - Answer -String A collection of characters in quotes is a __________ - Answer -String

What will be displayed if the following Java code segment is run? System.out.print("one "); System.out.println("two "); System.out.print("three "); A. one two three B. one two three C. one two three D. one two three - Answer -A. one two three Which of the following would Java recognize as a String? A. "4b0ut T!me" B. "word" C. "!@*" D. "45" E. ALL - Answer -E. ALL Strings are defined by __________ quotes - Answer -double What is wrong with the following code? String x; System.out.println(x); A. You cannot print the string without first assigning a value B. You cannot assign x as a variable name C. The line, System.out.println(x); should instead be System.out.println(x.toString()). D. The line, String x; will cause an error because it should be string x. E. System.out.println()should be replaced with system.out.print() - Answer -A. You cannot print the string without first assigning a value What is wrong with the following code? Scanner input = new Scanner(System.in); String s = nextLine(); A. Nothing is wrong, that code won't generate any errors. B. Should be: String s = input.next; C. Should be: String s = input.nextLine(); D. Should be: String s = next(); E. nextLine() method can't work with String variables - Answer -C. Should be: String s = input.nextLine();

C. System.out.println(greeting)(" user"); D. System.out.println(greeting - "user"); - Answer -B. System.out.println(greeting + " user"); To declare a String variable name and set it equal to "Lillian" you would type: A. String name = "Lillian"; B. String name = new "Lillian"; C. string name = "Lillian"; D. name = "Lillian"; - Answer -A. String name = "Lillian"; Which line of code correctly declares and initializes the variable x to a value of 3? A. Integer x = new Integer(x); B. x = 3 C. int x = 3; D. int x : 3; - Answer -C. int x = 3; Different data types can store different amounts of data. In the options below, which has the data types listed in order, from largest to smallest? A. double, String, boolean, int B. All the data types are the same size. C. int, String, double, boolean D. String, double, int, boolean E. int, double, boolean, String - Answer -D. String, double, int, boolean What data type should you use to hold whether a person passed or failed a class? A. boolean B. int C. double D. String - Answer -A. boolean Long is a data type that is ... longer... than an integer. It still is able to store whole numbers, but reserves a larger space in memory so that it can store a larger value. What is the largest value you can store in a variable that has the long data type? A. 2,147,483, B. 256 C. 1, D. 9,223,372,036,854,775,807 - Answer -D. 9,223,372,036,854,775, Consider the following code: long y = 9223372036854775807L; y = y + 1; System.out.println(y); What number does the program print when compiled and run? Hint: think about the max value you just looked up in your previous answer. A. -

B. 0

C. This program crashes and raises a longOverflow exception. D. 9223372036854775808 E. -9223372036854775808 - Answer -E. - Which of the following are examples of primitive data types? (Choose all that apply) A. int B. Arrays C. boolean D. double E. Scanner - Answer -A, C, D Int, Boolean, Double Which of the following correctly describes the difference between reference data types and primitive data types? A.Unlike primitive data types, reference data types cannot be stored by a variable. B. Primitive data types cannot be read by a Scanner object and stored using a variable. C. Primitive data types contain multiple pieces of data, while reference types only store one value at a time. D. Primitive data types are stored directly in a variable, while reference data type variables store a memory reference to where the data is stored. - Answer -D. Primitive data types are stored directly in a variable, while reference data type variables store a memory reference to where the data is stored. To output: Hi There You would need to use: System.out.______("Hi "); System.out.print("There"); What would correctly replace ______? A. output B. String C. print D. input E. println - Answer -C. print To declare an integer variable x and set it equal to 17 you would type: A. 17 = x; B. x = 17; C. int 17 = x; D. int = 17; E. int x = 17; - Answer -E. int x = 17; What is the most precise primitive data type for storing decimal values? A. double B. String

Put the following data types in order from largest to smallest (in terms of memory used). A. String, boolean, int, double B. String, double, boolean, int C. String, boolean, double, int D. String, double, int, boolean - Answer -D. String, double, int, boolean Assume the following has been typed in: Scanner scan = new Scanner(System.in); Which line of code declares a double y and sets it equal to input from the user? A. double y = nextDouble(); B. double y = scan.nextDouble() C. double y = scan.double(); D. double y = scan.nextDouble(); - Answer -D. double y = scan.nextDouble(); Which line of code declares a double variable z and sets it equal to 3.5? A. double z = 3.5; B. float z = 3.5; C. int z = 3.5; - Answer -A. double z = 3.5; An int is smaller in memory than a double? A. True B. false - Answer -A. True What is printed by the following code? int y = (13 - 7) / 4; System.out.println(y); A. 1 B. 11. C. 1.5 - Answer -A. 1 What is output when the following code is executed? double num = 2.0 * (26.75 - 20); System.out.println(num); - Answer -13. What is output when the following code is executed? double num = (0 - 3) * 5; System.out.println(num); - Answer --15. What is output when the following code is executed? int x = -7; x++; x++; x--; System.out.println(x); - Answer --

What is output when the following code is executed? int x = 13; x++; x++; x -= 7; x *= 3; x--; System.out.println(x); - Answer - What is x equal to after the following code has been executed: double x = 2.0; double y = 3.5; x = x + 2 * y; A. 7. B. 7. C. 9.0 - Answer -C. 9. What does x equal after the following code has been executed? int x = 13; x--; - Answer - What does x equal after the following code has been executed? int x = 16; x = x / 3; A. 6. B. 5 - Answer -B. 5 What is 61 % 4? A. 2 B. 4 C. 0 D. 3 E. 1 - Answer -E. 1 What is 13 % 7? A. 6 B. 3 C. 5 - Answer -A. 6 What is printed by the following code? int j = 33; j %= 6; System.out.println(j); A. 1

s = s % 60; - Answer -B. m = s / 60; s = s % 60; Which line of code properly casts a double to an integer? A. int n = (int) 6.71; B. int n = int(34.5); C. int n = (double) 6.23; - Answer -A. int n = (int) 6.71; What is the output when the following code is executed? int a = 93; double x = 89.86732; System.out.println((int) x); - Answer - What is the output when the following code is executed? int a = 93; double x = 89.86732; System.out.println(a / 3); - Answer - Which of the following is true about numeric casts in Java? A. There is a chance that you lose data when you convert from a larger data type to a smaller data type. B. There is a chance that you lose data when you convert from a smaller data type to a larger data type. C. It is required by binary storage. D. There is no reason to ever use casts unless you want to. E. Java is unable to determine the data types correctly at run time without it. - Answer - A. There is a chance that you lose data when you convert from a larger data type to a smaller data type. Executing which of the following lines of code would cause widening conversions to take place? Choose all answers that apply. A. double a = 19 / 7; B. double a = (double) 23 / 6; C. int a = 13.7 * 3; D. int a = (int) 11.4; - Answer -A/B Which of the following is not a primitive data type? A. boolean B. double C. int D. String - Answer -D. String What is output by the following code? int a = 11; System.out.println(a / 2);

A. 6.

B. 5.

C. 5 - Answer -C. 5 What is (19 % 7) * 2? A. 6 B. 10 C. 21 - Answer -B. 10 For which of the following would modular division be useful? A. All choices are correct B. identifying the digits of an integer C. testing whether numbers are even or odd D. time calculations using days and weeks E. money calculations using dollars and cents - Answer -A. All choices are correct When might you encounter a problem with integer overflow? A. When trying to store an integer value as a double B. When trying to store a decimal number in an int variable C. When trying to print an int in the middle of a String D. When trying to store an integer which is too big to be stored in an int variable - Answer -D. When trying to store an integer which is too big to be stored in an int variable Correct the following code so that p stores the nearest integer below 43.92. int p = 43.92; A. No changes, the code is fine. B. int p = double (43.92); C. int p = int (43.92); D. int p = (int) 43.92; - Answer -D. int p = (int) 43.92; Which of the following will print the tens column of an integer stored in x? A. System.out.print(x / 10 % 10); B. System.out.print(10 % x); C. System.out.print(x / 10); - Answer -A. System.out.print(x / 10 % 10); Which of the following lines of code create a String variable z and set it equal to the word "Strings are String-y"? A. z = (String) "Strings are String-y"; B. z = "Strings are String-y"; C. String z = "Strings are String-y"; - Answer -C. String z = "Strings are String-y"; Consider the following code: String a = "determined"; String b = "learner";

A. s = new String("Fast Start"); B. s = "Fast Start"; C. String s("Fast Start"); D. String s = "Fast Start"; - Answer -D. String s = "Fast Start"; Consider the following code: int x = 13; int y = 7; System.out.println(x % y); What is the output? A. 6 B. 13 % 7 C. 20 - Answer -A. 6 Which one of the following sentences is true? A. A double is a primitive data type so a double variable directly stores the data for the double. B. A double is a primitive data type so a double variable holds a reference to a location in memory. C. A double is a class data type so a double variable directly stores the data for the double. - Answer -A. A double is a primitive data type so a double variable directly stores the data for the double. Consider the following code: int x = 27; int y = 13; System.out.print("Answer: " + x + y); This code outputs: Answer: 2713 A. False B. True - Answer -B. True Consider the following code: System.out.println("Answer: " + (3 + 7)); This code outputs: Answer: 10 A. True B. False - Answer -A. True What will be printed by the following code? System.out.println("I came,\tI saw,\n\tI left."); A. I came, I saw, I left. B. I came, I saw, I left. C. I came, I saw, I left. - Answer -B. I came, I saw,

I left. To output: Hello World You would need to use what escape sequence in the blank: System.out.print("Hello ____ World"); A. \t B. \s C. \ D. \n - Answer -D. \n Which of the following symbols need an escape character to be output in a System.out.print statement? A.! B. # C. @ D. \ - Answer -D.
Which of the following commands will print the output below? / 'A. System.out.print("\\\/\n'\"); B. System.out.print("/\n'"); C. System.out.print("\\/\n\'"); D. System.out.print("\/\n'`"); - Answer -C. System.out.print("\/\n'`"); A special value that can be assigned to an object that has no value: - Answer -null The correct way to concatenate two Strings is ______. A. Using the combine keyword B. Using a comma between them C. Using the concat() function between them D. None of the items listed E. Using the String method merge - Answer -D. None of the items listed Use the following code for questions 2-7: String w1; String w2 = "ants"; String w3 = "flies"; What is stored in w1 by the following? w1 = "type: " + w2; A. type:ants B. type:fly C. Nothing, + does not work on Strings D. type: ants - Answer -D. type: ants

String s = "Animal"; System.out.println(s.indexOf("al")); A. 2 B. 1 C. 4 - Answer -C. 4 Suppose s1 and s2 are Strings and s1 = "a": s1 = s1.toLowerCase(); s2 = s1.toUpperCase(); System.out.println(s1.compareTo(s2)); What should be the expected output? A. 0 is printed B. A negative value is printed C. A positive value is printed - Answer -C. A positive value is printed Which of the following correctly declares a new primitive data-type variable? A. double d; B. d = new double(); C. new double; - Answer -A. double d; Which of the following best describes the relationship between a class and an object? A. Objects are created using classes, which are similar to blueprints. B. A class is a piece of data which is contained in an object. C. A class is a specific variable in a program, which has behaviors defined by objects. - Answer -A. Objects are created using classes, which are similar to blueprints. Select all the classes from the list A. 14 B. "Java" C. void D. int E. String F. 7. G. Scanner H. double - Answer -E, G Which code shows us creating an object of data type Scanner class and storing it with a variable named stat? A. stat Scanner = new Scanner(System.in); B. Scanner stat = new Scanner(System.in); C. new Scanner stat = new Scanner(System.in); D Scanner stat = Scanner(System.in); - Answer -B. Scanner stat = new Scanner(System.in); Consider the following code: String major = "Computer Science";

What is returned by the method call major.substring(1, 2)? A. "C" B. 'om' C. "Co" D. "o" - Answer -D. "o" A special value that means "no object" is called ______. A. class B. null C. void D. garbage collection E. reference - Answer -B. null What is output to the screen by the following code? System.out.println("Sum=" + 4 + 5); A. Sum=4 5 B. Sum= C. Sum= D. Sum=45 - Answer -D. Sum= Which of the following is true about classes and objects? A. A class is an instance of an object. B. It is possible to have many classes of the same object. C. Object variables have a reference pointing to their location in memory. D. Classes and object are the same exact thing and the terms can be used interchangeably. - Answer -C. Object variables have a reference pointing to their location in memory. Suppose s is a String which contains 5 characters. Which of the following calls will cause an error? A. s.substring(0); B. s.substring(0, 5); C. All the calls shown will cause an error D. s.substring(4, 5); E. None of the calls shown will cause an error - Answer -E. None of the calls shown will cause an error What does the following code do? String w3 = "aardvark"; System.out.println(w3.substring(w3.length()-2)); A. k B. Causes an index out of bounds error. C. rk D. ardvark E. aardvak - Answer -C. rk

B. Constructors for a class can be overloaded but they must have different numbers of parameters. C. Constructors for a class can be overloaded but they must all have a different number of parameters, different order of parameters or different types of parameters. - Answer - C. Constructors for a class can be overloaded but they must all have a different number of parameters, different order of parameters or different types of parameters. Where can a description of the accessible methods for a class be found? A. In the output produced by running the class B. In the API documentation for the relevant library C. In the .class file - Answer -B. In the API documentation for the relevant library What is the output when the following code is executed? Rectangle r1 = new Rectangle(3.0);r1.setLength(5.0);System.out.println(r1.getArea()); A. 15. B. 6. C. 25.0 - Answer -A. 15. Select all the primitive data types. A. double B. Circle C. Rectangle D. String E. int F. Scanner G. boolean - Answer -A, E, G What is the maximum value an Integer can hold? A. Integer.MaxValue() B. There is no limit to the value an Integer can hold. C. Integer.Max() D. Integer.MAX_VALUE - Answer -D. Integer.MAX_VALUE Which of the following is not a wrapper class? A. String B. Integer C. Double - Answer -A. String Which package do the wrapper classes belong to? A. java.lang B. java.util C. shapes - Answer -A. java.lang Given the following code, what would be the value stored in the Double object referenced by the variable d? int x = 7;

Double d = new Double(x); A. 8 B. 7 C. 7.0 - Answer -C. 7. Which code shows an example of autoboxing being used correctly? A. Integer c = Integer((int) 5.6); B. Double n = new Double(4.8); System.out.println(n); C. Integer b = new Integer(14); System.out.println(b.intValue()); D. Double m = 2.5; - Answer -D. Double m = 2.5; The Math.pow() method returns a value that is of the ______ data type. A. String B. boolean C. double D. int - Answer -C. double What is wrong with: int s = Math.sqrt(49); A. The 49 should be 49.0 since Math.sqrt takes a double type parameter. B. It needs a numeric cast since Math.sqrt returns a double data type. C. Nothing, the code is correct as written. D. Math.sqrt cannot take a number, it must be a variable. - Answer -B. It needs a numeric cast since Math.sqrt returns a double data type. According to the Java Quick reference Guide which of the following Math functions do you NOT need to know for the AP exam? A. pow B. abs C. random D. max - Answer -D. max Consider the following declaration Lego newBlock = new Lego(); Which of the following best describes the situation? A. Lego is an object of the newBlock class type B. Lego is a primitive value of the newBlock type C. Lego and newBlock are both classes D. newBlock is a primitive value of the Lego type - Answer -D. newBlock is a primitive value of the Lego type Which of the following is used to indicate a new comment? A. ' B. \t