CS 161. SAMPLE FINAL Fall 2014

Similar documents
CS111: PROGRAMMING LANGUAGE II

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Chapter 2: Data and Expressions

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

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

CompSci 125 Lecture 11

Introduction to Java Applications

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

H212 Introduction to Software Systems Honors

Chapter 2: Data and Expressions

ECE 122 Engineering Problem Solving with Java

Data Conversion & Scanner Class

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Programming with Java

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Practice Midterm 1 Answer Key

Chapter. Let's explore some other fundamental programming concepts

Peer Instruction 1. Elementary Programming

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Final Exam. COMP Summer I June 26, points

COMP102: Test July, 2006

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

PROGRAMMING FUNDAMENTALS

Reviewing all Topics this term

Full file at

This exam is open book. Each question is worth 3 points.

JAVA OPERATORS GENERAL

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Chapter 2: Basic Elements of Java

APCS Semester #1 Final Exam Practice Problems

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

AP CS Unit 3: Control Structures Notes

Mr. Monroe s Guide to Mastering Java Syntax

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

CS111: PROGRAMMING LANGUAGE II

Practice Midterm 1. Problem Points Score TOTAL 50

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Selection and Repetition Revisited

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Selection and Repetition Revisited

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Problem Grade Total

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

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

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

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

Java I/O and Control Structures

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

double float char In a method: final typename variablename = expression ;

JAVA Programming Fundamentals

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Basics of Java Programming variables, assignment, and input

Control Structures II. Repetition (Loops)

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Course Outline. Introduction to java

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

Chapter 3: Operators, Expressions and Type Conversion

Problem Solving With Loops

Introduction to Computer Science and Object-Oriented Programming

Operators & Expressions

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

I. Variables and Data Type week 3

CS 61B Data Structures and Programming Methodology. June David Sun

Darrell Bethea May 25, 2011

Program Fundamentals

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

Assignment 1 due Monday at 11:59pm

CS 101 Fall 2006 Midterm 3 Name: ID:

Selection Statements and operators

Java Coding 3. Over & over again!

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Java I/O and Control Structures Algorithms in everyday life

Fundamentals of Programming Data Types & Methods

ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

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

First Java Program - Output to the Screen

Introduction to Computer Science Unit 2. Exercises

COMP 202 Java in one week

Control Statements: Part 1

Java Programming Fundamentals. Visit for more.

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Repetition, Looping CS101

Full file at

CS110: PROGRAMMING LANGUAGE I

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

Selection and Repetition

Chapter 4: Writing Classes

ECE 122. Engineering Problem Solving with Java

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS 106 Introduction to Computer Science I

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Transcription:

CS 161 SAMPLE FINAL Fall 2014 1. What are the three parts of a loop? How are those three things organized in a while loop? How are they organized in a for loop? 2. Write the code to declare MyStuff to be a primitive double variable, initialized to 7.17867. 3. What are two visibility modifiers in Java? 4. There are two main kinds of Java programs, what are they? 5. Write the Java code for the following (variable names are in bold): A. Balance is assigned Balance plus Deposit. B. Counter is assigned Counter minus 1. C. Average is assigned Total divided by NumItems. D. Interest is assigned Balance multiplied by intrate. 6. What symbols are used for the following operators in Java? a. logical AND e. logical NOT i. relational NOT EQUAL b. logical OR f. integer division j. relational EQUAL c. assignment g. decrement k. remainder integer division d. increment h. conditional operator l. String concatenation 7. Evaluate the following expressions (as the computer would): a. 1 / 2 c. 3 * 2 e. Math.pow(2,4) b. 1 + 2 3 * 5/ 2 d. 15 % 4 f. Math.sqrt(81) 8. Given the following assignments: int j = 5, k=7; evaluate the following: a. j k b. ( j k) * ( j + k) c. j * ( k % 3) 9. When the following code is run, the user types in: The erroneous ways of Doctor No. What will be stored in each of the variables when the code is done running? Scanner scan = new Scanner (System.in); String Message, Word, nextword, HisWord; System.out.println( Enter a sentence please ); Message = scan.nextline(); Int num = Message.length(); Scanner Tokens = new Scanner(Message); Word = Tokens.next(); Char ans = Message.charAt(6) String myword = Word.toUpperCase(); nextword = Tokens.next(); HisWord= nextword.replace ( e, x );

10. Use the following code: Class LibraryBook { private String Title; private boolean Available; public LibraryBook (String Bookname) { Title = Bookname; Available = true; public void BorrowBook() { Available = false; public void ReturnBook() { Available = true; public void displaystatus() { System.out.print( \n + Title+ is ); If(! Available) System.out.print ( not ); System.out.println( available.\n ); a. What is the name of this class? b. List the names, if any, of the instance variables of this class: c. List the names, if any, of the constructors of this class: d. List the names, if any, of the methods of this class: e. Write the code to declare mybook to be an object of this class, having the title: The Joy of Java Write the code to call BorrowBook() for the object mybook. f. Write the code to declare another object of this class (you choose the name and book title). Then call the method DisplayStatus() for your object. g. Write the code to declare an array, called mylibrary, of 100 objects from this class. The objects are not initialized. 11. Write the number of times each loop repeats it iterative part (body of the loop)_. Assume that the variables index and n have been declared as integer variables. Also, zero, infinite, and unknown are perfectly valid answers: a. d. while (index< 10) for( index=0; index < 10; index++) System.out.println( index) ; System.out.println( index);

b. e. index = 0; n = 10; for( int index = 10; index >0; index++) while (index < n) System.out.println( index); System.out.println(index); index ++; c. f. index= 0; n = 0; index = 10; n =0; while( index <= 10) while( index > n) { n += index; { index --; index ++; System.out.println( index); 12. Admission rate to the local theater are based on age. Write an if/else statement to assign the correct value to the double variable Admit using the following table. Assume the variable age holds a positive integer value. Age Cost Under 12 4.00 12 58 6.50 Over 58 5.00 13. Write a for loop to print the integers from 1 to 100. Make sure the output is readable with 10 integers on a line and having a comma and space after each value. 14. Write a for loop to print your name 20 times, using a separate line for each. 15. Using a while loop, write the code to print the multiples of 5, starting with 5 times 1 and ending with 5 times 10. 16. Suppose yourfortune is an integer variable. Write a segment of Java code that uses a switch statement to print the following responses yourfortune Response 1: You will soon go on a long journey 2: You are wise beyond your years. 3: Today is your lucky day! 4: You will soon be very rich! Any other value Your future is not yet known.

17. Write a method that will print a row of asterisks (*). The number of asterisks printed will be determined by an integer parameter passed to the method. The method is public and does not return a value. 18. Suppose mystars is an object of the class to which the method in #17 belongs. Write a call to the method you wrote for #17 that will print 27 asterisks. 19. Write the lines of code necessary to declare the integer variable MyNum and give it a random value between 15 and 30. 20. Write a while loop to print the whole numbers starting at 50 and ending with 98. Re-write the loop using a for-loop instead. 21. Why is the main() method declared public and static?? 22. What is the difference between formal and actual parameters? 23. Change the following for loop to a while loop: for( int value = 0; value < 23; value ++) System.out.println( Hello + value); 24. Write the code that will print the message Wear a Coat! if the temperature is less than 40 and the wind is more than 15 mph. 25. Why is Java considered a strongly typed language? 26. What is a class and how does an object relate to the class? 27. Using the segment of code below a) Add code to convert the string to a character b) ADD a verification of input loop which will ensure that the value entered by the user is one of the options listed: c) Add a switch statement to print a different message for each type of car. Scanner scan = new Scanner(System.in); String Ans; System.out.println( What kind of car do you drive? ); System.out.prinltn( (F)ord, (C)hevy, (H)onda, (M)azda, (T)oyota, (B)uick: ); Ans = scan.next(); 28. Why does an Applet not need a main() method?

29. Write the code to declare an array of 50 integers, then fill the array with random values between 0 and 150; 30. Given an array of integers called MyScores, how do you determine the number of elements contained in the array? 31. The array Writing101 contains 40 integers, which are test scores. Write a method that will receive the array as a parameter, find the index of the lowest score in the array, and return that index. 32. What is the starting index of an array in Java? 33. Write the code to declare an array of 30 objects of the class Dog. 34. Write the code to declare array of integers, using an initializer list containing: 2,4,6,8,9,11,13. 35. As a programmer, to access the contents of the 5 th element stored in an array called MyArray, we use the code MyArray[4]. How does the computer actually find that element in memory? 36. Where on the screen is the coordinate <0,0>? 37. What are the predefined colors available in the Java Color class? Are more colors available? 38. What is a static method? Give an example of calling a static method. 39. Write the code to draw a rectangle from the point (35,45) that has one side of 65 and one of 75. Fill the rectangle with the color green. 40. What is a component? (Definition & Example) 41. What is an event? (Definition & Example) 42. What is a listener? (Definition & Example) 43. What is a dialog box? List three kinds of dialog boxes available in Java. 44. What is a label? 45. What is a text field? How is it different from a label? How is it different from a dialog box? 46. What is a button component? List several types of button components, their similarities, their differences and the events they generate. 47. What is the definition of a component? What is the definition of a container?

48. What three objects are necessary to create a Graphical User Interface in Java? 49. Provide a definition for each of the three objects you identified in #48. 50. How is a panel different from a frame? What is the default Layout Manager for a panel? 51. Write a method called SumRange that receives two integer parameters: First and Second. If First is greater than Second, the method returns zero. Otherwise it returns the Sum of ALL THE INTEGERS between First and Second, inclusive. 52. Write a method called SumOfEvens that receives two integer parameters, First and Second. If First is larger than Second, the method returns zero. Otherwise it returns the sum of the even integers between (but not including) First and Second. 53. Write a method called evenlydivisible that accepts two integer parameters and returns true if the first parameter is evenly divisible by the second, or visa-versa, and returns false otherwise. Return false if either parameter is zero. 54. Write a method called randominrange that receives two integer parameter, First and Second that define a range of integers. Return a random integer in the range defined by First and Second. 55. Write a method called MaxFive that receives five (5) integer parameters and returns the largest of the five. 56. Write a method called MinSix that receives six (6) integer parameters and returns the smallest of the six. 57. Write a method called Alarm that prints the string Alarm! multiple times, on separate lines. The method should accept an integer parameter that specifies how many times the string is printed. Print an error message if the parameter is less than 1. 58. Write a method called Sum100 that returns the sum of the integers from 1 to 100, inclusive. This method will receive no parameters and the sum. 59. Write the code to declare a new Color object using R,G,B values. 60. Write a method called RandomColor that receives NO parameters, but returns a Color object. The method should randomly generate an integer value for the Red, Green and Blue values of your color. 61. Evaluate the following code (as the computer would): a. 3 / 4 b. 9/2 c. (double) 3 / 4 d. (int) 4.791 * 5 e. 22 % 3 f. 18 / (6 2)+ 14 % 5 * 3