AP Computer Science A Unit 2. Exercises

Similar documents
Introduction to Computer Science Unit 2. Exercises

Computational Expression

Mr. Monroe s Guide to Mastering Java Syntax

Introduction to Computer Science Unit 2. Notes

AP Computer Science Unit 1. Programs

AP CS Unit 3: Control Structures Notes

Midterm Examination (MTA)

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

St. Edmund Preparatory High School Brooklyn, NY

Important Java terminology

Programming with Java

AP Computer Science Unit 1. Writing Programs Using BlueJ

Array. Array Declaration:

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

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

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

AP Computer Science Unit 1. Writing Programs Using BlueJ

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

PROGRAMMING FUNDAMENTALS

Introduction to Java & Fundamental Data Types

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

Getting started with Java

CS Week 5. Jim Williams, PhD

Java Programming Language. 0 A history

Java Bytecode (binary file)

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Introduction to Computer Science Unit 2. Notes

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Chapter 2: Basic Elements of Java

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Operators in java Operator operands.

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

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

Lecture Set 2: Starting Java

Following is the general form of a typical decision making structure found in most of the programming languages:

AP COMPUTER SCIENCE A

Lecture Set 2: Starting Java

Introduction to Java Unit 1. Using BlueJ to Write Programs

Software and Programming 1

Oct Decision Structures cont d

What did we talk about last time? Examples switch statements

CS-201 Introduction to Programming with Java

Chapter 3 Intro to Java

Chapter 2 Elementary Programming

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

COMP2121: Microprocessors and Interfacing. Number Systems

The Big Idea: Background: About Serial

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 02: Using Data

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

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

Introduction to Java Applications; Input/Output and Operators

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Program Fundamentals

Question: Total Points: Score:

JAVA Programming Fundamentals

CS11 Java. Fall Lecture 1

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

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

4 Programming Fundamentals. Introduction to Programming 1 1

CS 106 Introduction to Computer Science I

Datatypes, Variables, and Operations

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Zheng-Liang Lu Java Programming 45 / 79

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

CMSC131. Introduction to your Introduction to Java. Why Java?

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

APCS Semester #1 Final Exam Practice Problems

Welcome to the Using Objects lab!

Computer Programming, I. Laboratory Manual. Final Exam Solution

Fall 2017 CISC124 9/16/2017

At its simplest, a computer is a collection of wires, transistors and electrical components that are connected in a specific way When you send

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

H212 Introduction to Software Systems Honors

CMPT 125: Lecture 3 Data and Expressions

Full file at

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

Software and Programming 1

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Anatomy of a Java Program: Comments

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Fundamentals of Programming Data Types & Methods

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

Fundamentals of Programming. By Budditha Hettige

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Java Coding 3. Over & over again!

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

download instant at

CP122 Computer Science I. Chapter 2: Data Types, Variables, and I/O

Elementary Programming

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

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

COMP 110 Project 1 Programming Project Warm-Up Exercise

Transcription:

AP Computer Science A Unit 2. Exercises A common standard is 24-bit color where 8 bits are used to represent the amount of red light, 8 bits for green light, and 8 bits for blue light. It is the combination of these three colors that generates every other color you see on a monitor. 1. How many different levels of red can be represented using 8 bits? 2. How many different colors can be represented using 24 bits? Y2K refers to a computer problem from the previous century. In the early days of computers, memory (whether for storage or running a program) was very expensive. So, when recording dates, only the last two digits were saved. For example, the year 1985 would be saved as 85. This worked well enough for most programs at first but with the approach of the year 2000, it needed to be fixed; otherwise the year 2000 would be indistinguishable from the year 1900 (as both would be saved as 00). 3. How many bits are necessary to save a number from 0 to 99? 4. How many bits are necessary to save a number from 0 to 9999? 5. An Arduino Uno microcontroller can detect a voltage between 0 and 5 volts. How many bits does it use to represent the voltage if 1023 indicates 5 volts and 0 indicates 0 volts? Translate each binary number into a base ten number. 6) 1010 2 7) 0010 0001 2 8) 1111 2 9) 1000 0000 2 10) 1111 1111 2 11) 1000 1000 2 Translate each hexadecimal number into a base ten number 12) 31 16 13) 2F 16 14) 5D 16 15) A5 16 16) 59 16 17) 12 16 1

Translate each number into a base 10 number. If a number is incorrectly represented, and therefore cannot be translated, write INVALID. 18) 22 3 19) 80 7 20) 31 5 21) 123 4 22) 272 8 23) 55 6 Translate each base ten integer into a binary number. 24) 67 25) 100 26) 8 27) 12 28) 50 29) 88 Translate each base ten number into a hexadecimal number. 30) 30 31) 123 32) 8 33) 64 34) 54 35) 75 36) Convert this integer from base 10 to base 5 24 37) Convert this integer from base 10 to base 9 18 38) Convert this integer from base 10 to base 7 60 39) Convert this integer from base 10 to base 3 23 2

Any binary number can be easily converted to hexadecimal, and vice versa. Since 4 bits can represent a value between 0 and 15, each group of 4 bits can converted to the corresponding hex number. For example 1101 0100 = D4 because 1101 = D and 0100 = 4 40) This is why hex numbers are so common. For example, going back to the problems at the beginning of this packet, which is the easier way to specify a color? a) B16FCA b) 1011 0001 0110 1111 1100 1010 Translate each binary number into a hexadecimal number. 41) 0010 1011 2 42) 1010 0001 2 43) 1111 1101 2 44) 1011 0001 2 45) 0000 1011 1011 2 46) 1001 1001 1000 2 Translate each hexadecimal number into a binary. 47) FE 16 48) 200 16 49) 8 16 50) 4F0 16 51) A0 16 52) B03 16 Run the following program in BlueJ to answer questions 53 to 56. import javax.swing.jcolorchooser; import java.awt.color; public class MyColorChooser { public static void main(string[] args) { Color c1 = Color.ORANGE; Color c2 = JColorChooser.showDialog( null,"pick a Color", c1 ); System.out.println( c2.tostring() ); 53. Click on the RGB tab and set Red to 255, Green to 255, Blue to 0, and Alpha to 255. What background color of the sample text?. 3

54. Change the color code to 00FF00. What is the background color of the sample text? Why? 55. If the color code is 000000, what is the background color of the text? 56. If color1 is CC0000 and color2 is 990000, which color is darker? Try to figure this out first then check by entering those numbers. 57. Java code is saved in a text file that has a.class extension. True False 58. To run Java byte code on a particular computer, you must install a java compiler. True False 59. The JVM detects run-time errors. True False 60. The JVM detects logic errors. True False 61. The Java compiler translates text into Byte code. True False 62. The JVM translates byte code into the machine language of the particular device it is installed on. True False 63. A bit consists of 8 bytes. True False 64. The same Java class files can be run on a variety of computers (e.g. those running Windows, Linux, etc.) True False 65) Is the following binary number even or odd? 0111 0100 1011 100 1100 1011 1000 1110 1010 0101 There is a way to figure this out without converting it to a base 10 integer. 66) Let s say you ve written a program that looks something like this: public class Light{ public static void main(string[] args) { // some great code and I say, email me the code. Which file(s) should you send me? a) Light.java b) Light.ctxt c) Light.class d) All of the above files e) Any of the first three files. 4

67) ASCII (American Standard Code for Information Interchange), started in the 1960 s, was a very common 7 bit encoding scheme for letters, numbers, etc. What was the total number of characters that it could represent? Numbers 65 to 90 represented the uppercase letters A to Z. Numbers 97 to 112 represented the lowercase letters a to z. Convert 65 (base ten) to binary Convert 97 (base ten) to binary How many bits have to be changed to convert a lowercase letter to an uppercase letter (or vice versa)? 68) Numeric literals can be represented in binary form by putting 0b or 0B in front (that s a zero, not an O). However, the value of an int is still displayed in base 10. What is displayed? 69) Putting 0x, or 0X, before a numeric literal means that it is a hexadecimal value. What is displayed? int x = 0b11; int y = 0b11; int c = x + y; System.out.println( c ); int x = 0xF; int y = 0x2; int c = x + y; System.out.println( c ); 70) What is displayed? int x = 0b1110; int y = 0x12; int c = x + y; System.out.println( c ); 71) What is displayed? int num1 = 0b0010; int num2 = 0x10; int result = num1 * num2; System.out.println( result ); Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement.) 72. If the user enters 82, what is displayed? 73. If the user enters 2, what is displayed? Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextint(); if ( x < 7 ) { System.out.print( "A" ); System.out.print( "B" ); System.out.print( "C" ); 5

74. If the user enters 10, what is displayed? 75. If the user enters 12, what is displayed? 76. Are the curly braces required in this code? 77. If the user enters 10, what is displayed? 78. If the user enters 4, what is displayed? 79. If the user enters 2, what is displayed? 80. If x has an initial value of 33, what is its final 81. If x has an initial value of 62, what is its final Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextint(); System.out.print( "AA" ); if ( 11 >= x ) { System.out.print( "BB" ); System.out.print( "CC" ); Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextint(); if ( x!= 10 ) x = x + 3; if ( x > 7 ) x = x + 12; x = x - 1; System.out.println( x ); // x is declared and assigned a value if ( x > 30 && x <= 50 ) x = x + 10; x = x - 4; if ( x < 40 && x > 60 ) x = x + 2; 82. If x has a value of -5 and y has a value of 63, what is displayed? 83. If x has a value of 47 and y has a value of 47, what is displayed? 84. Select the TRUE statement. a) H is never printed. b) H is always printed. c) H is only printed sometimes. // x and y are declared and initialized if ( x > 30 y >= 60 ) System.out.print( "G" ); if ( x < 100 x > 40 ) System.out.print( "H" ); if ( y < 10 y > 50 ) System.out.print( "K" ); 6

85. If y has an initial value of 12, what is displayed? 86. If y has an initial value of 26, what is displayed? 87. If y has an initial value of 7, what is displayed? 88. The code to the right does not compile. The is highlighted and the error message is: without if Why? 89. Given that the above error is fixed by the addition of two characters, what will be displayed if the user enters 12? 90. If x has an initial value of -8, what is its final 91. If x has an initial value of 9, what is its final 92. If x has an initial value of 15, what is its final 93. If x has an initial value of 22, what is its final 94. If x has a final value of 6, what was its initial Scanner x = new Scanner( System.in ); System.out.println( "Number?" ); int y = x.nextint(); if ( y > 10 ) y += 10; if ( y > 20 ) y++; System.out.println( y ); Scanner read = new Scanner( System.in ); System.out.println( "Number?" ); int a = read.nextint(); if ( a > 10 && a < 20 ) a = 2 * a; System.out.println( a ); a--; System.out.println( a ); // x is declared and assigned a value if ( x > 10 x < 20 ) x = x + 2; if ( x < 10 x > 20 ) x = x + 3; // x is declared and assigned a value if ( x > 10 && x < 20 ) x = x + 2; if ( x < 10 && x > 20 ) x = x + 3; if ( x > 10 && x > 20 ) x = x + 10; 95. If the user enters "38", what is displayed? 96. If the user enters -4, what is displayed? 97. If the user enters 99, what is displayed? Scanner read; read = new Scanner(System.in); System.out.print( "Enter a number: " ); int num = read.nextint(); if ( num < 13) { System.out.println( "ZZ" ); if ( num < 20) { System.out.println( "YY" ); if ( num < 40 ) { System.out.println( "XX" ); System.out.println( "WW" ); 7

98. If k has an initial value of 13, what is its final 99. If k has an initial value of 22, what is its final 100. If k has an initial value of 4, what is its final // k is declared and assigned a value if ( k < 5 ) k += 2; if ( k < 10 ) k += 5; if ( k < 20 ) k += 6; 101. If k has an initial value of 10, what is its final 102. If k has an initial value of 30, what is its final 1 2 3 4 // k is declared and assigned a value if ( k < 11 ) k += 4; if ( k < 40 ) k++; 103. Lines 7 and 8 can be deleted without changing how the code runs. TRUE FALSE 5 6 7 8 if ( k > 11 ) k = k - 2; if ( k > 22 ) k -= 4; 104. If x has an initial value of 8, what is its final 105. If x has an initial value of 11, what is its final 106. If x has an initial value of 19, what is its final // x is declared and assigned a value if ( x > 10 ) x = x + 2; if ( x < 5 ) x++; if ( x <= 20 ) x = 2*x; if ( x > 5 ) x++; 107. short is an integer data type that is two bytes long. char is also two bytes long but its values are interpreted as characters. The same bits are stored in both variables. What is displayed? short a = 0b0000000001000001; char b = 0b0000000001000001; System.out.println( a ); System.out.println( b ); You will not be tested on shorts or bytes. 8

if ( x > 5 ){ if ( x < 10 ) System.out.println("YES"); if ( x > 5 && x < 10 ) System.out.println("YES"); 108. Do the above two code snippets produce the same results? If they do not, what value of x will cause different results? if ( x > 5 ) System.out.println("NO"); if ( x < 2 ) System.out.println("NO"); System.out.println("MAYBE"); if ( x < 2 x > 5 ) System.out.println("NO"); System.out.println("MAYBE"); 109. Do the above two code snippets produce the same results? If they do not, what value of x will cause different results? if ( x > 20 ) { if ( x < 50 ) System.out.println("ONE"); System.out.println("TWO"); if ( x > 20 && x < 50 ) System.out.println("ONE"); System.out.println("TWO"); 110. Do the above two code snippets produce the same results? If they do not, what value of x will cause different results? 9