AP CS Fall Semester Final

Similar documents
int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Java Review. Fundamentals of Computer Science

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Introduction to Programming Using Java (98-388)

PROGRAMMING FUNDAMENTALS

Lexical Considerations

CMPT 125: Lecture 3 Data and Expressions

Full file at

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

JAVA Programming Fundamentals

Java Bytecode (binary file)

IEEE Floating-Point Representation 1

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

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

Lecture Set 4: More About Methods and More About Operators

Lexical Considerations

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

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

Language Reference Manual

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Zheng-Liang Lu Java Programming 45 / 79

CT 229 Java Syntax Continued

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Chapter 3: Operators, Expressions and Type Conversion

Place your name tag here

VARIABLES AND TYPES CITS1001

Computer Programming CS F111

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

DATA TYPES AND EXPRESSIONS

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Accelerating Information Technology Innovation

Control Structures in Java if-else and switch

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 Lexical Considerations

AP Computer Science A

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Variables, Types, Operations on Numbers

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Casting. References. References

Programming with Java

String Computation Program

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 2: Data and Expressions

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

The for Loop. Lesson 11

CIS October 16, 2018

Lexical Structure (Chapter 3, JLS)

printf( Please enter another number: ); scanf( %d, &num2);

Java Object Oriented Design. CSC207 Fall 2014

MODULE 02: BASIC COMPUTATION IN JAVA

Java Programming Fundamentals. Visit for more.

Chapter 2: Data and Expressions

XC Total Max Score Grader

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Java Notes. 10th ICSE. Saravanan Ganesh

Computer Programming I - Unit 5 Lecture page 1 of 14

Java Programming Basics. COMP 401, Spring 2017 Lecture 2

Lecture Set 4: More About Methods and More About Operators

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

B.V. Patel Institute of BMC & IT, UTU 2014

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Important Java terminology

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

2 nd Week Lecture Notes

Chapter 6 Primitive types

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CS260 Intro to Java & Android 03.Java Language Basics

Java Identifiers, Data Types & Variables

CS11 Java. Fall Lecture 1

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Full file at

CS/ENGRD 2110 SPRING 2018

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

What did we talk about last time? Examples switch statements

Peer Instruction 1. Elementary Programming

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Loops. CSE 114, Computer Science 1 Stony Brook University

EXPRESSIONS AND ASSIGNMENT CITS1001

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Transcription:

Name: Class: Date: AP CS Fall Semester Final Multiple Choice Identify the choice that best completes the statement or answers the question. 1. What is printed by the following code? int k = 3; for (int pj = 5; pj <= 100; pj++) k = pj + 1; System.out.println(3 * k + pj); a. Won t compile b. Never prints, endless loop c. 3082 d. 5820 2. What is printed by the following code? int jj = 2; do int gk = 1 + jj++; while(jj < 10); System.out.println(gk); a. Won t compile b. Never prints, endless loop c. 45 d. 55 3. String sss = I like school ; Using the two paramater form of substring, which line of code will print the last character of sss? 4. String sss = I like school ; Using the one paramater form of substring, which line of code will print the last character of sss? a. System.out.println( sss.substring(11) ); b. System.out.println( sss.substring(12) ); c. System.out.println( sss.substring(13) ); d. System.out.println( sss.substring(12, 14) ); 5. String sss = I like school ; Using charat, which line of code will print the last character of sss? a. System.out.println( sss.charat(11) ); b. System.out.println( sss.charat(12) ); c. System.out.println( sss.charat(13) ); d. System.out.println( sss.charat(14) ); 6. Assume s is a String. Using the two-parameter form of substring, which line of code will print the last character of s? a. System.out.println( s.substring( s.length( ) - 1, s.length( ) ) ); b. System.out.println( s.substring( s.length( ), s.length( ) ) ); c. System.out.println( s.substring( s.length( ) + 1, s.length( ) ) ); d. System.out.println( s.substring( s.length( ) - 1, s.length ) ); a. System.out.println( sss.substring(12, 13) ); b. System.out.println( sss.substring(12) ); c. System.out.println( sss.substring(12, 11) ); d. System.out.println( sss.substring(12, 14) ); 1

Name: 7. Assume s is a String. Using the one-parameter form of substring, which line of code will print the last character of s? a. System.out.println( s.substring(s.length( ) - 1 ) ); b. System.out.println( s.substring(s.length( ) + 1 ) ); c. System.out.println( s.substring(s.length( ) ) ); d. System.out.println( s.substring(s.length - 1 ) ); 8. Assume s is a String. Using charat, which line of code will print the last character of s? a. System.out.println( s.charat(s.length 1 ) ); b. System.out.println( s.charat(s.length( ) ) ); c. System.out.println( s.charat(s.length( ) +1 ) ); d. System.out.println( s.charat(s.length( ) 1 ) ); 9. What is printed by the following? String p = ABCDE ; System.out.println( X + p.substring(2,2) + X ); a. Illegal, won t compile b. XBX c. XCX d. XX 10. What s printed by the code below? int k = 23; System.out.println(Integer.toBinaryString( k )); a. 10101 b. 11111 c. 10110 d. 10011 11. What code that will printout the hex equivalent of int m? a. System.out.println( Integer.toString(m, hex ) ); b. System.out.println( Integer.toHexString(m) ); c. System.out.println( Integer.toString(m) ); d. System.out.println( Integer.toString(m, 16) ); 12. What code that will printout the octal equivalent of int m? a. System.out.println( Integer.toString(m, oct ) ); b. System.out.println( Integer.toOctalString(m) ); c. System.out.println( Integer.toString(m) ); d. System.out.println( Integer.toString(m, 8) ); 13. What is printed by the following? String s1 = "m"; String s2 = "Z"; System.out.println(s1.compareTo(s2)); a. A positive number b. A negative number c. 0 d. true e. false 14. What is printed by the following? int x = 9; double y = 1.3; String s = Felix ; System.out.println(x + y + s + x + y); a. 91.3Felix91.3 b. 10.3Felix91.3 c. 91.3Felix10.3 d. Illegal, won t compile 2

Name: 15. What code that will print the following? Cat\"hello"X'm' a. System.out.println("Cat//\"hello\"X\'m\'"); b. System.out.println("Cat///"hello/"X\'m/'"); c. System.out.println("Cat\\\"hello\"X\'m\'"); d. System.out.println("Cat\"hello"X'm'"); 16. What are all objects equal to before they are initialized? a. null b. zero c. d. Illegal. They must be initialized when declared. 17. What code replaces <##> below so as to make it skip the remaining code in the loop, but still continue looping? for(j = 0; j < 10; j++) if (j >= 5) <##> m = j *2; m =j + 2; sum = sum + m; a. continue; b. break; c. exit; d. stop; 18. What is printed by the following code? int x = 5; double y = (x >8)? Math.pow(x,2) : Math.pow(x,3); System.out.println(y); a. Illegal, won t compile b. 125 c. 125.0 d. 1.25E0 19. What is printed by the following code? int x = 462; if(!x > 0) System.out.println( positive ); else System.out.println( negative ); a. Illegal, won t compile b. positive c. negative d. Nothing is printed 20. What is printed by the following? System.out.println(17.2%4); a. 1.2 b. 4.3 c. 1 d. 1.0 3

Name: 21. Does the conditional part of the if below return a true, false, or give an error? a. true b. false c. error int x = 3, y = 5; if ( (x < 8 ) && (y > -1) ) some code 22. Of which of the following is y *= z + m the equivalent? a. y = y * (z + m); b. y = y * z + m; c. Neither of these 23. What is printed by the following? System.out.println(-12%5); a. 2 b. -2 c. Illegal, won t compile d. -2.4 24. What is printed by the following? System.out.println( 12%(-5) ); a. 2 b. -2 c. Illegal, won t compile d. -2.4 25. What is printed by the following? System.out.println( -12%(-5) ); a. 2 b. -2 c. Illegal, won t compile d. 2.4 26. What is printed by the following? int x = 7; int y = 3; double d = (double)(x/y); System.out.println(d); a. 2 b. 2.0 c. 2.3333333333333 d. Illegal, won t compile 27. What is printed by the following? int x = 7; int y = 3; double d = (double)x/y; System.out.println(d); a. 2 b. 2.0 c. 2.33333333333333 d. Illegal, won t compile 28. Specify the accessibility provided by public: a. Accessible from anywhere b. Accessible from just within its own class c. All methods of classes in the same package can access the feature. This is the default access control if none is specified. d. Access is permitted by methods of the same class, subclasses, and classes in the same package. 29. Specify the accessibility provided by private: a. Accessible from anywhere b. Accessible from just within its own class c. All methods of classes in the same package can access the feature. This is the default access control if none is specified. d. Access is permitted by methods of the same class, subclasses, and classes in the same package. 4

Name: 30. What is printed by System.out.println(10 / 4 * 50); a..05 b. 125 c. 125.0 d. 100 31. What code will convert double d to a String and store the result in s. a. String s = (String)d; b. String s = d + ; c. String s = + d; d. Both A and C e. Both B and C 32. What does the following print? double p = 3.07; double q = 2.9; int count = 2; if ( (p / q == 1) && (count-- < 59))... System.out.println(count); a. 1 b. 2 c. 3 d. 0 33. What does the following print? double p = 3.07; double q = 2.9; int count = 2; if ( (p / q == 1) (count-- < 59))... System.out.println(count); a. 1 b. 2 c. 3 d. 0 34. What does the following print? double p = 3.07; double q = 3.07; int count = 2; if ( (p / q == 1) && (count-- < 59))... System.out.println(count); a. 1 b. 2 c. 3 d. Illegal, won t compile e. Not enough information 35. Suppose you wish to find the index of the first occurrence of the character A (starting with index 4) in String mystring. Which line of code will store this value in the integer k. a. int k = mystring.indexof(65, 4); b. int k = mystring.indexof( A, 4); c. int k = mystring.lastindexof(65); d. int k = mystring.charat(65, 4); e. Both A and B 36. Which line of code will find the first occurrence of ee in the sentence stored in String sentc. Store the index in jj. a. int jj = sentc.indexof(ascii(69), 2); b. int jj = sentc.indexof(69,69); c. int jj = sentc.indexof( ee ); d. int jj = sentc.index( ee ); 37. What is another name for method? a. function b. parameters c. signature d. header 38. What is another name for header? a. function b. parameters c. signature d. method 5

Name: 39. What is another name for function? a. method b. parameters c. signature d. header 40. What is another name for signature? a. function b. parameters c. method d. header 41. To what is a numeric data member automatically initialized (in the absence of specific initialization)? a. 0 or 0.0 b. 1 c. null d. Nothing, not initialized 42. To what is a local numeric variable automatically initialized (in the absence of specific initialization)? a. 0 or 0.0 b. 1 c. null d. Nothing, not initialized 43. When Boolean OR-ing two boolean expressions, when is only one expression evaluated? a. When the left expression is true b. When the right expression is true c. When the left expression is false d. When the right expression is false 44. When Boolean AND-ing two boolean expressions, when is only one expression evaluated? 45. In order to determine if the character at index j of String sp is a whitespace character, which of the following would return the appropriate boolean? a. Character.sp.isWhitespace(j) b. sp.charat(j).iswhitespace c. Character.isWhitespace(sp.charAt(j)) d. sp.charat(iswhitespace( )) 46. If c is of type char and i is of type int, is the following legal or illegal? i = c; a. Legal b. Illegal 47. If c is of type char and i is of type int, is the following legal or illegal? a. Legal b. Illegal c = (char)i; 48. If c is of type char and i is of type int, is the following legal or illegal? c = i; a. Legal b. Illegal 49. What is the range of ASCII codes for the following? The digits 0,1-9 a. 48-57 b. 49-58 c. 65-74 d. 97-106 a. When the left expression is true b. When the right expression is true c. When the left expression is false d. When the right expression is false 6

Name: 50. What is the range of ASCII codes for the following? The letters a z a. 48-73 b. 49-58 c. 65-90 d. 97-122 51. What is the range of ASCII codes for the following? The letters A Z a. 48-73 b. 49-58 c. 65-90 d. 97-122 52. What is returned by Math.ceil(-1032.13)? a. -1032.0 b. -1033.0 c. -1032 d. -1033 53. What is returned by Math.floor(150.788)? a. 150.8 b. 150.7 c. 150.79 d. 150.78 54. What is returned by Math.round(7/2)? a. 4 b. 3.5 c. 3.0 d. 3 7

AP CS Fall Semester Final Answer Section MULTIPLE CHOICE 1. ANS: A The scope of pj is limited to inside the loop, starting with for and ending with. 2. ANS: A The scope of gk is limited to inside the loop, starting with do and ending with. 3. ANS: A Play around with these in BlueJ s Code Pad (View Show Code Pad) 4. ANS: B 5. ANS: B 6. ANS: A 7. ANS: A 8. ANS: D 9. ANS: D Start at index 2, up to but not including index 2, is an empty String: 10. ANS: E...answer is 10111 11. ANS: E...both B and D 12. ANS: E...both B and D 13. ANS: A See ASCII table for lesson on char. Think of compareto as s1 - s2, in that order. 14. ANS: B + signs are evaluated left to right. x+y evaluates to 10.3. 10.3 + Felix results in a String, 10.3Felix. Append x to the end results in 10.3Felix9. Concatenate y results in choice b. 1

15. ANS: C \,,, etc. are escape chacters. They have special meanings. To include them in Strings, you have to put a backslash (\) in front. 16. ANS: A 17. ANS: A continue means skipping the rest of a loop, straight to the step expression, j++. 18. ANS: C The second line can be rewritten as: double y; if (x > 8) y = Math.pow(x,2); else y = Math.pow(x,3); 19. ANS: A! has top priority here. (Unary operators, like! and -, usually have precedent). Should be (!(x>0)). 20. ANS: A The multiple of 4 closest to 17.2 is 16. 17.2-16=1.2 21. ANS: A 22. ANS: A 23. ANS: B Closest multiple of 5 to -12 is -10. 24. ANS: A Closest multiple of -5 to 12 is 10. Another way of thinking is, if the questions asks 10%(-5), the answer is 0, because (-5)(-2) = 10. 25. ANS: B Same as (-12)%(-5). 26. ANS: B...The parenthesis around x/y creates a little world of its own and since x and y are both integers, integer arithmetic is done giving 2 (not 2.33333333333333). This answer is then cast into a double. 2

27. ANS: C...Only the x is cast as a double. x/y is now done with double arithmetic since x is a double. Casting has high precedence. 28. ANS: A 29. ANS: B 30. ANS: D * and /, + and _, have same precedence, so evaluate from left to right. int/int = int. 31. ANS: E Primitive types can t be cast into objects like String, or vice versa. 32. ANS: B p/q is not equal to 1. So false && (...) will never be true. Java short-circuits and skips the whole if statement. It doesn t bother to evaluate count-- < 59, because it doesn t matter. So count remains the same value. 33. ANS: A p/q is not equal to 1. So the value of the if condition depends on whether count-- < 59. BTW, avoid writing programs like this unless you re an expert at the differences between x-- and --x. It actually compares count < 59, then do count--. 34. ANS: E p/q is 1.0. Well, it could also be 1.00000001 or 0.99999997. Floating point numbers are not stored exactly in most programming languages. So it s a really bad idea to compare them to integers. Include a margin of error. 35. ANS: E... both A and B 36. ANS: C Strings are objects. No ASCII table. 37. ANS: A... both A and B 38. ANS: C. 3

39. ANS: A... both A and B 40. ANS: D 41. ANS: A A Circle class has an instance variable radius. A BankAccount class has an object variable balance and name. (These are the same names for data member. They are declared inside the class but outside all the methods.) radius and balance are set to default values if not initialized. 42. ANS: D Local variables are declared inside a method. In the circle class s area() method, we may temporarily store the area into a new variable double a before returning it. But if we don t initialize a, it doesn t have any values. The compiler would complain. 43. ANS: A Look up short-circuiting. 44. ANS: C Look up short-circuiting. 45. ANS: C 46. ANS: A char goes from 0 to 127, so no problem storing it as an int. 47. ANS: A i could be 65 ( A ), or 5000. This code will always compile, but the latter would result in a run-time exception. 48. ANS: B 49. ANS: A 50. ANS: D 51. ANS: C 52. ANS: A What is the smallest number greater than (less negative) -1032? Return it as a double. 53. ANS: E... answer is 150.0 4

54. ANS: C 7/2 is integer arithmetic and gives 3. After rounding, it s still 3.0 5