Last Name: Circle One: OCW Non-OCW

Similar documents
First Name: AITI 2004: Make-up Exam 1 July 9, Last Name:

Introduction to Programming Using Java (98-388)

First Name: AITI 2004: Exam 2 July 19, 2004

Java is an objet-oriented programming language providing features that support

Question: Total Points: Score:

PROGRAMMING FUNDAMENTALS

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

University of Massachusetts Amherst, Electrical and Computer Engineering

University of Palestine. Mid Exam Total Grade: 100

Selected Questions from by Nageshwara Rao

Question: Total Points: Score:

Building Java Programs

CSC Java Programming, Fall Java Data Types and Control Constructs

CS 101 Fall 2005 Midterm 2 Name: ID:

Course Outline. Introduction to java

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

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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

Lec 3. Compilers, Debugging, Hello World, and Variables

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Java Foundations Certified Junior Associate

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Final Exam. COMP Summer I June 26, points

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Getting started with Java

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

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

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

First Name: AITI 2004: Exam 2 July 19, 2004

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

MIT AITI Lecture 18 Collections - Part 1

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Building Java Programs

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

Building Java Programs

Place your name tag here

3. Java - Language Constructs I

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

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

Computer Science II (20082) Week 1: Review and Inheritance

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

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

Question: Total Points: Score:

CS 1331 Fall 2016 Exam 2

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

The Java language has a wide variety of modifiers, including the following:

CS111: PROGRAMMING LANGUAGE II

CS 177 Spring 2010 Exam I

Programming Basics. Digital Urban Visualization. People as Flows. ia

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Object-Oriented Programming

Midterm Exam 2 CS 455, Spring 2014

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

CS171:Introduction to Computer Science II

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

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

CS 231 Data Structures and Algorithms, Fall 2016

Lecture 9: Lists. MIT-AITI Kenya 2005

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Distributed Systems Recitation 1. Tamim Jabban

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

I have neither given nor received any assistance in the taking of this exam.

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Crash Course Review Only. Please use online Jasmit Singh 2

Midterm Exam CS 251, Intermediate Programming March 12, 2014

COMP 401 Spring 2013 Midterm 1

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Final Exam COMP Fall 2004 Dec 16, 2004

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Practice Midterm 1. Problem Points Score TOTAL 50

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSCE 206: Structured Programming in C++

MIDTERM REVIEW. midterminformation.htm

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

Fundamentals of Programming Data Types & Methods

CMP 326 Midterm Fall 2015

Question: Total Points: Score:

APCS Semester #1 Final Exam Practice Problems

Question: Total Points: Score:

CS 1331 Exam 1 ANSWER KEY

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CS251L REVIEW Derek Trumbo UNM

CSE 142 Su 04 Computer Programming 1 - Java. Objects

I. True/False: (2 points each)

PIC 10A. Final Review: Part I

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

Computer Programming, I. Laboratory Manual. Final Exam Solution

Transcription:

First Name: AITI 2004: Exam 1 June 30, 2004 Last Name: Circle One: OCW Non-OCW Read Instructions Carefully! This is a 3 hour closed book exam. No calculators are allowed. Please write clearly if we cannot understand your answer, you will receive 0 points for that question. This exam has 6 parts worth a total of 100 points. Section 1 Java Basics (20 points) 1. (2 points) ( T / F ) Java programs must be compiled separately for each platform. 2. (2 point) ( T / F ) In Java, the identifiers Bob and bob refer to the same variable. 3. (2 points) The command we use to compile Java code is a. java b. javac 4. (2 points) The command we use to run a Java program is a. java b. javac 5. (2 points) Which of the following are valid variable names? a. true6 b. you-me c. hello Java d. DEGREE 6. (3 points) Which of the following are valid statements? a. int i = 5 b. short s = 5; c. boolean firsttime; d. char c = '4'; 7. (2 points) What is the value of the following arithmetic expressions? a. (4 + 3) * 6 2 = 40 b. 14 % (18 / 5) = 2 8. (5 points) What is the value of the following Boolean expressions? boolean b1 = false; boolean b2 = true; a. b1 && b2 = false b. b1 b2 = true c. b2 b1 = true d.!b1 = true e. (!b1) && b2 = true Page 1 of 7 1

Section 2: Control Structures (15 points) 1. (2 points) What does the following code print to the screen? int score = 75; String orange = "apple"; String apple = "orange"; if (score >= 60) System.out.println(apple); else System.out.println(orange); a. apple b. orange 2. (2 points) What is the output of the following code? char grade = 'D'; switch (grade) { case 'A': System.out.println("Excellent"); break; case 'B': System.out.println("Good"); break; case 'C': System.out.println("OK"); break; default: System.out.println("Let's talk"); Let's Talk 3. (5 points) Write a for loop that calculates the sum of the first 100 multiples of 3, (3, 6, 9, ). (Note: This is not the same as the sum of the multiples of 3 from 1 to 100) int sum = 0; for(int i = 1; i <= 100; i++) { sum += 3 * i; 4. What is the output for the following segment of the code? a) int k = 10; while (k < 10) { k += 2; no output System.out.println(k); b) int k = 10; 12 while (k < 20) { 14 k += 2; 16 System.out.println(k); 18 20 Page 2 of 7 2

Section 3: Methods and Arrays (10 points) 1. (5 points) Write a method, product, which takes an integer array as an argument and returns the product of the numbers in that array. (It multiplies the elements of the array). // static keyword is optional public static int product(int[] nums) { int p = 1; for(int i = 0; i < nums.length; i++) { p *= nums[i]; return p; 2. (5 points) The following piece of code calls a method called scaleproduct. The first argument to scaleproduct is an array of integers and the second is an integer. The method takes the product of the array and returns the sum of the product and the second argument. int[] numbers = {1,2,3,1; int scale = 3; int ans = scaleproduct(numbers, scale); //1*2*3*1 + 3 System.out.print(ans); Output: 9 Write the scaleproduct method. To take the product of the array, it should call the product method you wrote above. public static int scaleproduct(int[] nums, int scale) { return product(nums) + scale; Page 3 of 7 3

Section 4 Lists and Iterators (15 points) 1.(4 points) List one advantage and one disadvantage of using an ArrayList compared to an array. One advantage of using an ArrayList over an array is that it will automatically resize for you as necessary, and it will do so in an optimal fashion. One disadvantage of using an ArrayList is that it cannot store primitives, so you have to wrap your primitives in objects before adding them to the ArrayList, and then extract the primitive value from these objects when you access them from the ArrayList. 2. (4 points) Would an ArrayList or a LinkedList be more appropriate for the following applications? - To store the people waiting in line at a bank, where a lot of people are continually joining the line at the end and leaving from the front, but you never want to search or reorder the line. a) ArrayList b) LinkedList - To store the grade books of a fixed number of students in this class that you want to update. a) ArrayList b) LinkedList 3. (7 points) Using an Iterator, complete the method below that takes the product of all the integers in the given ArrayList. You can assume java.util.* is imported. (Hint: The Integer class has a method with signature: public int intvalue()) static int product(arraylist intlist) { int product = 1; Iterator intiter = intlist.iterator(); while(intiter.hasnext()) { int i = ((Integer)intIter.next()).intValue(); product *= i; return product; Page 4 of 7 4

Section 5 Packages Access and Scope (8 points) class TestScope { int x = 0; int y = 0; void f() { int y; //LINE A y = 20; x = 10; void print() { System.out.println(x); f(); System.out.println(x); System.out.println(y); 1. (4 points) What will this code print out? 0 10 0 2. (4 points) What will this code print out if we remove LINE A? 0 10 20 Section 5 Static and Final (12 points) 1. (4 points) Add two public constants of type double to the Thermometer class below, one for the freezing point and one for the boiling point in Celsius. The freezing point is 0 degrees Celsius and the boiling point is 100 degrees Celsius. 2. (4 points) Add a public static method to the thermometer class to convert degrees Celsius to degrees Fahrenheit. It should accept a double argument in Celsius and return a double in Fahrenheit. The conversion is Fahrenheit = 1.8 * Celsius + 32. package thermo; public class Thermometer { public static final double FREEZING_POINT = 0.0; public static final double BOILING_POINT = 100.0; public static double convert(double celsius) { return 1.8 * celsius + 32; Page 5 of 7 5

3. (4 points) In the main method of UseThermometer, use the Thermometer class to calculate and print the boiling point in degrees Fahrenheit. package thermo; public class UseThermometer { public static void main(string args[]) { double fahrenheit = Thermometer.convert(Thermometer.BOILING_POINT); System.out.println(fahrenheit); Section 6 Classes (20 points) 1. On the next page write a class to represent a football team in a package called football. We want to be able to access the class FootballTeam from any package. The team has the following properties, which cannot be accessed outside of the class. - name of the team - number of wins - number of losses Write a constructor that accepts the name of the team, the number of wins, and the number of losses as arguments and sets the class properties to those values. This constructor should be accessible from any package. Write a second constructor that takes only the name of the team as an argument. This constructor should set the name of the team to the argument and the set the number of wins and losses to 0. (Hint: The body of this constructor should be one line and it should call the first constructor.) This constructor should be accessible from any package. Write methods that return the name of the team, the number of the wins, and the number of losses. These methods should be accessible from any package. Next write a method to increase the numbers of wins by 1 and another method to increase the number of losses by one. These methods should only be accessible from inside the football package. Write a method that returns true when a team has a "good" record, meaning the team has more wins than losses. This method should be accessible from any package. Finally, add a main method to the FootballTeam class. In the main method, construct a FootballTeam named "AITI" with 3 wins and 5 losses. Call the method that returns true when the team has a good record and print out the result. Now make three calls to the method that increases the number of wins by 1. Lastly, call the "good record" method again and print out the result. Page 6 of 7 6

package football; public class FootballTeam { private String name; private int numwins, numlosses; public FootballTeam(String name, int numwins, int numlosses) { this.name = name; this.numwins = numwins; this.numlosses = numlosses; public FootballTeam(String name) { this(name, 0, 0); public String getname() { return name; public int getnumwins() { return numwins; public int getnumlosses() { return numlosses; void addwin() { numwins++; void addloss() { numlosses++; public boolean hasgoodrecord() { return numwins > numlosses; public static void main(string[] args) { FootballTeam aiti = new FootballTeam("AITI", 3, 5); System.out.println("Good record? " + aiti.hasgoodrecord()); aiti.addwin(); aiti.addwin(); aiti.addwin(); System.out.println("Good record? " + aiti.hasgoodrecord()); Page 7 of 7 7