Some Sample AP Computer Science A Questions - Solutions

Similar documents
Example: Monte Carlo Simulation 1

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Iteration statements - Loops

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Controls Structure for Repetition

JAVA OPERATORS GENERAL

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selenium Class 9 - Java Operators

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

1. An operation in which an overall value is computed incrementally, often using a loop.

Selected Questions from by Nageshwara Rao

Building Java Programs

Chapter 4: Control structures. Repetition

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Loops. CSE 114, Computer Science 1 Stony Brook University

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

COS 126 Midterm 1 Written Exam, Fall 2009

Write a program which converts all lowercase letter in a sentence to uppercase.

CS1150 Principles of Computer Science Loops (Part II)

Building Java Programs Chapter 2

Chapter 4: Control structures

McGill University School of Computer Science COMP-202A Introduction to Computing 1

PROGRAMMING FUNDAMENTALS

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Some Practice Midterm Problems

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Exercise (Revisited)

Lecture Set 4: More About Methods and More About Operators

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Building Java Programs

Fundamentals of Programming Data Types & Methods

Building Java Programs

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Recitation: Loop Jul 7, 2008

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Introduction to the Java Basics: Control Flow Statements

Prof. Navrati Saxena TA: Rochak Sachan

Building Java Programs

Building Java Programs

CIS March 1, 2018

Midterm Examination (MTA)

Recap: Assignment as an Operator CS 112 Introduction to Programming

CIS October 19, 2017

CS 101 Spring 2007 Midterm 2 Name: ID:

Chapter 4: Control Structures I

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Repetition, Looping. While Loop

Chapter 3. Selections

What does this print?

AP COMPUTER SCIENCE A

b. Suppose you enter input from the console, when you run the program. What is the output?

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CS 112 Introduction to Programming

CS 101 Fall 2006 Midterm 3 Name: ID:

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Operators in java Operator operands.

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Expressions & Flow Control

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Java Programming for Selenium

AP Computer Science Unit 1. Programs

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Final Examination Semester 2 / Year 2011

Visual Programming. Lecture 2: More types, Methods, Conditionals

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

AP Computer Science Unit 1. Writing Programs Using BlueJ

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Lecture Set 4: More About Methods and More About Operators

CS 101 Fall 2005 Midterm 2 Name: ID:

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

H212 Introduction to Software Systems Honors

COS 126 Midterm 1 Written Exam Fall 2012

Building Java Programs

Building Java Programs

JAVA Programming Language Homework II Student ID: Name:

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Building Java Programs

Oct Decision Structures cont d

Key Java Simple Data Types

Variables and data types

Research Group. 2: More types, Methods, Conditionals

Expression statements are formed by adding a semicolon to the end of certain types of expressions.

1 Short Answer (10 Points Each)

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

CS171:Introduction to Computer Science II

Introduction to Java & Fundamental Data Types

Operators Questions

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Question 1 [20 points]

Transcription:

Some Sample AP Computer Science A Questions - s Note: These aren't from actual AP tests. I've created these questions based on looking at actual AP tests. Also, in cases where it's not necessary to have choices, I've removed the choices and changed the format of the question to short answer. Short Answer Questions 1) What does the following segment of Java code print out? double x = 4.5; int y = (int)x; System.out.println(x+" "+y); This prints out: 4.5 4 When you cast a double to an int, it truncates the result. 2) What does the following segment of Java code print out? int x = 7, y = 3; int z = x/y + x%y; if (z == x) y++; if (z == y) x++; System.out.println(x+" "+y+" "+z); This prints out: 8 3 3 Since 7/3 = 2 and 7%3 = 1, initially we set z to 3. At this point z and x aren't equal, so the first if gets skipped. Then we evaluate the second if where z and y are equal and x gets incremented to 8 (from 7). So, the final values of x, y and z are 8, 3, and 3 respectively.

3) What does the following segment of Java code print out? for (int i=1; i<30; i = i + 3) { if (i%2 == 0) System.out.print((i/2)+" "); System.out.println(); As we go through the loop, i takes on the values 1, 4, 7, 10, 13, 16, 19, 22, 25 and 28. Of these, the even values (those that pass the if statement test) are 4, 10, 16, 22 and 28. If we divide each of these by 2, we get the output: 2 5 8 11 14 4) What does the following segment of Java code do in general? System.out.println("Enter n."); int n = stdin.nextint(); int total = 0; while (n > 0) { total = total + n%10; n = n/10; System.out.println(total); For any value of n, n%10 isolates the units digit and n/10 chops off the units digit. Thus, the while loop systematically chops off each digit, adding it to total, until no more digits remain. Thus, this segment of code prints out the sum of the digits of the original value of n entered by the user.

Multiple Choice Questions 1) Which of the following Boolean expressions are equivalent? (Assume that x and y are integer variables that have been initialized with the intended values.) i) ((x > 0) && (y > 0)) ((x > 0) && (y < 0)) ii) x!= y iii) (x > 0) && (y!= 0) iv) (x > 0) && (x + y!= x) A) i and ii B) i and iii C) i, iii, iv D) ii, iii, iv E) All 4 are different, logically. When we look at the first expression, we see that x > 0 is a requirement, since this must be true to satisfy either of the larger clauses. Notice that once we force x > 0, as long as y isn't 0, the first expression is true, since the or allows either option. Once we have that down, we can look at the other choices. The second choice isn't equivalent because could just use x = -3, y = 4 to show that (i) and (ii) are different. In this case, the (i) is false while (ii) is true. Notice that (iii) is exactly what we determined (i) to be. Finally, notice that x + y!= x is equivalent to y!= 0, which we can show by subtracting x from both sides of the check. This, the correct answer is C. 2) The Boolean expression!(a B) is equivalent to which of the following? A)!A!B B)!A B C)!A &&!B D)!A && B E) None of the Above If we plug in the four options for the ordered pair (A, B) of (True, True), (True, False), (False, True) and (False, False), we see that the given expression is ONLY true for (False, False). Of the ones given, only choice C is ONLY true for (False, False), since it's an AND. Thus, the correct answer is choice C. (Note: This is an application of DeMorgan's Law.)

3) What does the following code segment print out? for (int i=5; i>0; i--) { for (int j=0; j<i; j++) System.out.print((2*i-j)+" "); System.out.println(); A) 5 4 3 2 1 B) 10 9 8 7 6 C) 10 8 6 4 2 D) 0 2 4 6 8 4 3 2 1 8 7 6 5 8 6 4 2 0 2 4 6 3 2 1 6 5 4 6 4 2 0 2 4 2 1 4 3 4 2 0 2 1 2 2 0 E) None of the Above As the outer loop runs, i counts down 5, 4, 3, 2, and 1. The value of 2*i in these situations is 10, 8, 6, 4 and 2, respectively. The inner loop counts up from 0 to i-1, so when i is 5, j assumes the values of 0, 1, 2, 3 and 4. Thus, what gets printed on the first line is 10 9 8 7 6. Tracing through the rest of the lines, we see that choice B is indeed correct.

Free Response Questions - s in Separate Attached Files (numtriangle.java, revdigits.java, drop.java) 1) Write two different segments of code that, assuming that the integer variable n has been assigned to a positive value, print out the following: 1 2 3 4 n 1 2..n-1 1 2 1 2) Complete the program below so that it prints out the digits of the integer n, in reverse order. For example, if the user enters 73546, then your program should print out 64537. (Hint: one of the short answer questions may shed light on how to solve this problem!) import java.util.*; public class revdigits { public static void main(string[] args) { System.out.println("Enter n."); int n = stdin.nextint(); // Fill in your code here! 3) The formula for how far an object falls from a height of h, in t sections is 16t 2 feet. Thus, the object's height off the ground after t seconds is equal to max(0, h - 16t 2 ). Complete the program below so that it prints out a chart of how high off the ground an object is at each second (starting at t = 0) until the object hits the ground. import java.util.*; public class drop { public static void main(string[] args) { System.out.println("Enter the initial height."); int height = stdin.nextint(); // Fill in your code here!