APCS Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Write the method main heading: It must be exactly as Java needs it to be for your code to compile. Hint: * below are the words you'll need. static main (String[] args) public void

public static void main(String[] args)

Consider the following code segment: if (n != 0 && x / n > 100) statement1; else statement2; If n is of type int and has a value of 0 when the segment is executed, what will happen? An ArithmeticException will be thrown. A syntax error will occur. statement1, but not statement2, will execute. statement2, but not statement1, will execute. Neither statement1 nor statement2 will be executed; control will pass to the first statement following the if statement.

statement2, but not statement1, will execute.

Consider the following code segment. int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment? (A) Value is: 21 (B) Value is: 2.3333333 (C) Value is: 2 (D) Value is: 0 (E) Value is: 1

(C) Value is: 2

Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = 1; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; } Which of the following is printed as a result of executing the following statement? System.out.println(mystery(6)); (A) [3, 4, 7, 12, 19, 28] (B) [3, 4, 7, 12, 19, 28, 39] (C) [4, 7, 12, 19, 28, 39] (D) [39, 28, 19, 12, 7, 4] (E) [39, 28, 19, 12, 7, 4, 3]

(C) [4, 7, 12, 19, 28, 39]

What is the output of the following code snippet? public static void main(String[ ] args) { int num = 100; if(num != 100) { System.out.println("Not 100"); } else { System.out.println("100"); } } Not 100 100 There is no output due to compiling errors Hello World

100

What value is stored in result if: int result = 13 - 3 % 2; 0 12 1 13

12

Evaluate the following expression. Make sure that your answer is correct type. For instance, if the answer is 2 and the type is int, then you should enter 2. If the answer is 2, and the type is double, then enter 2.0. 13 + Math.abs(-7) - Math.pow(2.0, 3) + 5

17.0

What is the output of the following code snippet? public static void main(String[ ] args) { int s1 = 20; if(s1 < 20) { System.out.print("1"); } if(s1 <= 40) { System.out.print("2"); } if(s1 > 20) { System.out.print("3"); } } 2 1 3 123

2

What is the output of the following code snippet, if the input is 25? public static void main(String[ ] args) { Scanner cin = new Scanner(System.in); System.out.print("Please enter a number: "); int i = cin.nextInt( ); if(i > 24) { i++; } else { i--; } System.out.println(i); } 24 25 26 27

26

What is the value of the price variable after the following code snippet is executed? int price = 42; if(price < 45) { price = price + 10; } if(price < 30) { price = price * 2; } if(price < 100) price = price - 20; } 52 32 64 84

32

What will be output as a result of the method call whatsIt(347)? 74 347 743 47

347

Evaluate the following expression. Make sure that your answer is correct type. For instance, if the answer is 2 and the type is int, then you should enter 2. If the answer is 2, and the type is double, then enter 2.0. 26 % 10 % 4 * 3

6

Evaluate the result from evaluating the following integer expression. Make sure that your answer is the correct type. For instance, if the answer is 2 and the type is int, then you should enter 2. If the answer is 2, and the type is double, then enter 2.0. (12 + 3) / 4 * 2

6

First question from the 2014 APCS Exam: Consider the following method. public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 2) x = x + arr[k]; return x; } Assume that the array nums has been declared and initialized as follows. int[] nums = {3, 6, 1, 0, 1, 4, 2}; What value will be returned as a result of the call mystery(nums) ? (A) 5 (B) 6 (C) 7 (D) 10 (E) 17

7

Refer to the following method. public static int mystery(int n) { if (n == 1) return 9; else return 3 * mystery(n - 1); } What value does mystery(1) return? 12 9 3 27 81

9

True or False: The following statement, written in the interactions pane, will compile and print to the console. System.out.println("said Sally. "I've said so." See?); A. False B. True

A. False

The statement System.out.print ("No" + (2 + 3) + "way"); produces the output: A. No5way B. No 2 3 way C. No23way D. None. Illegal statement

A. No5way

Based on the code below: String sentence; String str1, str2, str3; int length1, length2; sentence = "Today is Wednesday."; str1 = sentence.substring(9, 18); str2 = str1.substring(0, 3); str3 = sentence.replace('d', '*'); length1 = sentence.length(); length2 = str1.length(); System.out.print(str3); A. To*ay is We*nes*ay. B. Today * Wednesday C. Today is Wednesday D. **d** ** **d***d**

A. To*ay is We*nes*ay.

Each memory cell has a unique location in main memory, called the _________. A. address B. package name C. Scanner D. Java library

A. address

Which of these words that can appear in a Java program are not a reserved word? *Hint: It does not show up in blue text in Dr. Java. A. crossett B. int C. void D. class

A. crosset

Which of these must appear outside the body of a class? A. import statements B. constructors C. variable definitions D. instance variables

A. import statements

Which of the following statements correctly creates a Scanner object for keyboard input? A. Keyboard scanner = new Keyboard(System.in); B. Scanner keyboard = new Scanner(System.in); C. Scanner cin = new Scanner(System.Keyboard); D. Scanner keyboard(System.in);

B. Scanner keyboard = new Scanner(System.in);

Which of the following is not a primitive data type? A. int B. String C. double What is a primitive data type? A. int B. String C. double

B. String A. int C. double

What is the output of the following code snippet? public static void main(String[ ] args) { double income = 45000; double cutoff = 55000; double min_income = 30000; if(min_income > income) { System.out.println("Minimum income requirements not met."); } if(cutoff < income) { System.out.println("Income requirement is met."); } else { System.out.println("Maximum income limit is exceeded."); } } There is no output Maximum income is exceeded Income requirement is met Minimum income requirement is not med

Maximum income is exceeded

What is the output of the following Java code? int x = 0; if(x < 1) System.out.print("One "); System.out.print("Two "); System.out.print("Three"); Two Three' Two One Two Three None of these

One Two Three

When will the result be equal to 3? public static int whatIsIt(int x, int y) { int result = 0; if (x > y) result = 4; else result = 3; return result; } Only when x < y Only when x <= y Only when x > y For all values of x and y This method will never return

Only when x <= y

Consider the method public String mystery(String s) { String s1 = s.substring(0,1); String s2 = s.substring(1, s.length() - 1); String s3 = s.substring(s.length() - 1); if (s.length() <= 3) return s3 + s2 + s1; else return s1 + s2 + s3; } DELIVERY DELIVER RELIVED REVILED DLEIEVR

RELIVED

What is the syntax error in the following method definition? public static area(double r) { double a; a = 3.14 * r * r; return r * r; } The variable a is set but never used The method does not specify a return type. The method does not return a value of a The value returned does not match the specific variable of a

The method does not specify a return type.

True or False: All of the following statements are true in Java. 12 % 5 is 2 12 / 5 is 2 The names int and double describe the type of variable being initialized or declared. True False

True

Consider the following code segment. int x = 7; double y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment? Value is: 21 Value is: 2.3333333 Value is: 0 Value is: 2 Value is: 1

Value is: 2.3333333

What is the name of the following Scanner object? Scanner cin = new Scanner(System.in) new Scanner info cin

cin

Let's say you're just starting to write a new program. Assume there are no import statements or comments. What goes at the top of the new class between the words public and the class name? Fill in the blank: public _________ HuggyBear { } static main domain class

class

To join two String objects together, use the _____________________ operation. boolean concatenation logical conditional

concatenation

If you use the String searching functions to search for a particular character or substring, and the value you're searching for is not found, the methods will return: a. 0 b. "" c. False d. -1

d. -1

Consider the following output. 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 Which of the following code segments will produce this output? for (int j = 1; j <= 5; j++) { for (int k = 1; k <= 5; k++) { System.out.print(j + " "); } System.out.println(); } } b. for (int j = 1; j <= 5; j++) { for (int k = 5; k <= j; k--) { System.out.print(j + " "); } System.out.println(); } } c. for (int j = 1; j <= 5; j++) { for (int k = 1; k <= j; k++) { System.out.print(j + " "); } System.out.println(); } }

for (int j = 1; j <= 5; j++) { for (int k = 1; k <= 5; k++) { System.out.print(j + " "); } System.out.println(); } }

Which of the following will correctly check to see if the variable x is equal to 5? if (x > 5) if (x == 5) if (x = 5) if (x < 5)

if (x == 5)

Consider the following method definition: public static void printTopHalf() { } The word printTopHalf : is known as the access specifier is called the method name is needed if the method is called from main() surrounds the parameter list surrounds the method body is called the return type

is called the method name

Refer to the following code fragment below: public int class Size = 36; class Size refers to ______________________. the name of the variable it's ability to be used by other methods the type of variable the value of the variable

the name of the variable

In a variable definition, the kind of value stored inside variable name is called its: (Hint: The 'kinds' we use in AP Computer Science are int, double, and String.) type assesnment method class

type

Assume that x and y are boolean variables and have been properly initialized. (x || y) && x Which of the following always evaluates to the same value as the expression above? x y x && y x != y

x

The statement System.out.print("No" + (2 + 3) + "way") produces the output: A. No+2+3+way B. No5way C. No23way D. NoNowaywayway

B. No5way

Which of the following represents correct / implementation / code for the constructor in the card class? public class Card { private String mySuit; private int myValue; //0 to 12 public Card(String suit, int value) { / implementation / } public String getSuit() { return mySuit; } public int getValue() { return myValue; } public String toString() { String faceValue = " "; if (myValue == 11) faceValue = "J"; else if (myValue == 12) faceValue = "Q"; else if (myValue == 0) faceValue = "K"; else if (myValue == 1) faceValue = "A"; if(myValue >= 2 && myValue <= 10) return myValue + " of " + mySuit; else return faceValue + " of " + mySuit; } } public class Desk { private Card[] myDeck; public final static int NUMCARDS = 52; public Deck() { ... //Simulate shuffling the deck. public void shuffle() { ... //other methods not shown... } A. mySuit = suit; myValue = value; B. suit = mySuit; value = myValue; C. Card = new Card(mySuit, myValue);

A. mySuit = suit; myValue = value;

In a main method definition, the symbols appearing inside the ( ) are called the: A. parameter list B. methods C. getters D. setters

A. parameter list

Read the following excerpt from the Syllabus to answer the following question: Because the class relies on doing your homework before class, you can't really succeed by staying up all night every Sunday. Instead, you need to budget your time. Spend at least 30 minutes every day, reading the material, and 30 minutes in the computer lab, or on your computer at home, working on problems. For most students, the best way to learn to program is to actually write programs. Spend time writing and running small "demo" programs to explore the behavior of what you are learning. The homework readings and programming assignments are outlined in the schedule. Feel free to work ahead. Since the schedule clearly outlines the homework due dates, there is no reason to turn in 'late work'. The best way to learn Java is to: Spend at least 30 minutes every day reading the material in the textbook. Spend at least 30 minutes in the computer lab working on problems. Actually write programs using the information we have learned. All of the above.

All of the above.

Which statement about the Quadrilateral class is false? The perimeter and area methods are abstract because there's no suitable default code for them. The getLabels method is not abstract because any subclasses of Quadrilateral will have the same code for this method. If the Quadrilateral class is used in a program, it must be used as a super-class for at least one other class. No instances of a Quadrilateral object can be created in a program. Any subclasses of the Quadrilateral class must provide implementation code for the perimeter and area methods.

Any subclasses of the Quadrilateral class must provide implementation code for the perimeter and area methods.

The elements inside the class body are surrounded with, or delimited by: A. Brackets[ ], B. Braces{ }, C. Parenthesis( ), D. Quotations" "?

B. Braces{ },

What is the output of the following Java code? int x = 0; if(x < 0) System.out.print("Four "); System.out.print("Five"); System.out.print("Six"); A. Five B. Five Six C. Four Five Six D. None of the these

B. Five Six

What is the computer component that stores program instructions during a program execution? A. Hard Drive B. Memory C. Capacitors D. CPU

B. Memory

In the assignment statement below, what is the purpose of the (double) y? int x = 3, y = 2; double z = x /(double) y; A. To double the amount of the answer, like multiplying by 2. B. To make sure that the result of the division is a decimal number. C. To round the answer to the nearest integer. D. To round the answer to the nearest tenth.

B. To make sure that the result of the division is a decimal number.

Which one of the following is a correct method of declaring and initializing an integer variable with name value? A. int value; B. int value = 30; C. double crossett = 30; D. value = 30;

B. int value = 30;

Which of the following represents correct /* implementation */ code for the constructor in the Card class? A. this.mySuit = getSuit( ); this.myValue = getValue( ); B. mySuit = suit; myValue = value;

B. mySuit = suit; myValue = value;

public static void main(String[ ] args) { double bottles = 2.0; double bottles_volume = bottles * 2; System.out.println(bottles_volume); } A. 0 B. 4 C. 4.0 D. compile-time error

C. 4.0

Read the following excerpt from the Syllabus to answer the following question: Homework: Everybody learns in different ways and at different speeds; in general though, if you're an average student and want to receive an average (C-B) grade, you should plan on spending about 7 hours each week outside of class in addition to the five hours of classroom time. If you have difficulty with the material, or if you want to receive an A in the course, you'll probably have to spend more time. If an average student wants to end up with an average B or C in this course, how much time should the student spend working on Java outside of class in addition to the 5 hours per week in the classroom? A. 5 B. One hour each day, six days a week. C. 7

C. 7

What does the following code fragment print to the console? System.out.println("C:\\Windows\\my.ini\\"); A. C:\Windows\my.ini B. "C:\\Windows\\my.ini" C. C:\Windows\my.ini\ D. C:\\Windows\\my.ini

C. C:\Windows\my.ini\

The following line of code is going to print out what type of value? System.out.print("575"); A. double B. int C. String

C. String

Programs that are designed to "work for you", carrying out a task you specify, are known as: A. utility programs B. applets C. application programs D. worker programs

C. application programs

When you define the method, create a ______________ for each value you want to pass into the procedure. A. instance variable B. local variable C. formal parameter D. actual parameter

C. formal parameter

The devices that feed data and programs into computers are called ______. A. processing devices B. feedback devices C. input devices D. output devices

C. input devices

Refer to the following code fragment: double answer = 13 / 5; System.out.print("13 / 5 = " + answer); The output is 13 / 5 = 2.0 The programmer intends the output to be 13 / 5 = 2.6 Which of the following replacements for the first line of code will not fix the problem? A. double answer = 13.0 / 5.0; B. double answer = 13 / 5.0; C. int answer = (13 / 5); D. double answer = 13.0 / 5;

C. int answer = (13/5);

In Java, Scanner is a class in which package? A. java.lang B. java.text C. java.util D. extends Scanner

C. java.util

If your java source code program compiles, but displays an error message when you run it, then it must have a: A. syntax error B. logical error C. runtime error D. compile-time error

C. runtime error

What does the following line of code print out to the console? System.out.println("\"\\ / \\\\ // \\\\\\ ///\""); A. // \ //// \ ////// \" B. \ / \\ // \\\ /// C."\ / \\ // \\\ ///" D. \\\\\\ /// \\\\ // \\ / E. \\ / \\\\ // \\\\\\ ///

C."\ / \\ // \\\ ///"

Which of the following code fragments will cause an error? A.String greeting = "Hello, World!"; B. String greeting = "Hello, Dave!"; C.System.out.println(Hi + "5"); D. int luckyNumber = 5; System.out.println(luckyNumber);

C.System.out.println(Hi + "5");

Look at the following use of the substring() method: String word = "Hello World!"; String subs = word.substring(1,2); The String subs will contain: A. "H" B. "He" C. "el" D. "e"

D. "e"

Suppose x = 2 and y = 3. If the statement x *= y; is executed once, what is the value of x? A. 2 B. 3 C. 5 D. 6

D. 6

What is the output of the following code segment? int myAge = 63; int kathysAge = myAge; kathy's Age = 60; System.out.print(myAge); A. 60 B. myAge C. Kathy's Age D. 63

D. 63

When a Java string does not have surrounding double quotes, what type of error occurs? A. Logical error B. Syntax error C.Run time D. Compile-time error

D. Compile-time error

Which of these is a legal (that is, syntactically correct) Java class name? A. 2_U B. U-2 C. 2U D. U2

D. U2

Text, like everything else in your computer, is stored as raw binary numbers. To treat those numbers as characters, they must be encoded. The encoding scheme used in Java is called: A. SHA-256 B. RSA C. EBCDIC D. Unicode

D. Unicode

Consider the following method definition: public static void printTopHalf() { } The word static : A. is called the return type B. is called the method name C. surrounds the parameter list D. is needed if the method is called from main() E. surrounds the method body F. is known as the access specifier

D. is needed if the method is called from main()

When defining a function, (unlike a procedure), you must specify a ____________. A. return statement B. access specifier C. parameter list D. return type

D. return type

Consider this hierarchy, in which Novel and Textbook are subclasses of Book. Which of the following is a true statement about the classes shown? The Textbook class can not have private instance variables that are in neither Book nor Novel. Each of the classes - Book, Novel, and Textbook - can have a method computeShelfLife, whose code in Book and Novel is identical, but different from the code in Textbook

Each of the classes - Book, Novel, and Textbook - can have a method computeShelfLife, whose code in Book and Novel is identical, but different from the code in Textbook

Which of the following variable names is an example of a constant? FEET_PER_SECOND feetPerSecond FeetPerSecond final

FEET_PER_SECOND

Consider the following declarations in a client class. You may assume that ClassOne and ClassTwo have default constructors. ClassOne c1 = new ClassOne(); ClassOne c2 = new ClassTwo(); Which of the following method calls will cause an error? I. c1.methodTwo(); II. c2.methodTwo(); III. c2.methodOne(); None I only II only III only I and II only

I and II only

Which of the following correctly initializes an array arr to contain four elements each with value 0? I. int[] arr = {0, 0, 0, 0}; II int[] arr = new int[4]; III int[] arr = new int [4]; for (int i = 0; i < arr.length; i++) arr[i] = 0;

I, II, and III

Which of the following will execute without throwing an exception/error? I. String s = null; String t = ""; if (s.equals(t)) System.out.println("empty strings?"); II. String s = "0"; String t = "moly"; if (s.equals(t)) System.out.println("holy moly!"); III. String s = "holy" String t = s.substring(4); System.out.println(s + t); I only II only III only I and II only II and III only

II and III only

Which represents correct /* implementation code */ for the Rectangle constructor? I super(labels); II super(labels, topLeft, bothRight); III super(labels); myTopLeft = topLeft; myBotRight = botRight;

III only

Which of these is a legal (that is, syntactically correct) Java class name?

J6

Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi". EXAMPLES: makeAbba("Hi", "Bye") → "HiByeByeHi" makeAbba("Yo", "Alice") → "YoAliceAliceYo" makeAbba("What", "Up") → "WhatUpUpWhat" public String makeAbba(String a, String b) { String result = ""; result = _________________ return result; } What would result need to be in order to finish this function? a + b + b +a; "Hello " + name + "!"; first + last + last + first; b + a + a + b; last + first + first + last;

a + b + b +a;

If a, b, and c are integers, which of the following conditions is sufficient to guarantee that the expression a < c || a < b && !(a == c) evaluates to true? a < c a < b a > b a == b a == c

a < c


Set pelajaran terkait

Educational Assessment Pre-Assessment

View Set

GLM Chapter 7 - Supply Management

View Set

Real Estate Principles, Chapter 2

View Set

personality psychology chapter 7 physiological approach

View Set

❤️🕸❤️🕸22 Chapter 16 test quest🧚🏼‍♀️💗🧚🏼‍♀️

View Set

Algebra 2 final exam study guide

View Set

Chapter: Completing the Application, Underwriting, and Delivering the Policy

View Set