CS 1331 Exam 1 ANSWER KEY

Similar documents
CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 1331 Fall 2016 Exam 2

CS 1331 Fall 2016 Exam 3 Part 1 ANSWER KEY

Exam Percentage: / 55 = %

Introduction to Programming Using Java (98-388)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CS 1331 Exam 3 Practice ANSWER KEY

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

CS 1331 Exam 3 Practice

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Distributed Systems Recitation 1. Tamim Jabban

Question: Total Points: Score:

ANSWER KEY. Study Guide. Completely fill in the box corresponding to your answer choice for each question.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

Selected Questions from by Nageshwara Rao

Introduction to Java

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

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

( &% class MyClass { }

Recitation 3 Class and Objects

PROGRAMMING FUNDAMENTALS

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

public class Q extends P { private int num; private int i; P(int n) { public Q() { this.num = n;

Distributed Systems Recitation 1. Tamim Jabban

3. Java - Language Constructs I

Lecture 02, Fall 2018 Friday September 7

Array. Prepared By - Rifat Shahriyar

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS 101 Spring 2007 Midterm 2 Name: ID:

CS162 Computer Science I Fall 2018 Practice Exam 1 DRAFT (9 Oct.)

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS 1322 Test 2 - Version 1 Tuesday June 24 th

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

CS 1316 Exam 1 Summer 2009

CSC 1214: Object-Oriented Programming

Informatik II (D-ITET) Tutorial 6

Java Magistère BFA

Java Bytecode (binary file)

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

Problem Grade Total

CS 231 Data Structures and Algorithms, Fall 2016

CS 113 MIDTERM EXAM 2 SPRING 2013

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

3.1 Class Declaration

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

COMP 401 Spring 2013 Midterm 1

Decision Making in C

MIDTERM REVIEW. midterminformation.htm

CMSC 331 Second Midterm Exam

Static, Final & Memory Management

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

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Lecture 2: Java & Javadoc

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

CHAPTER 7 OBJECTS AND CLASSES

BM214E Object Oriented Programming Lecture 6. Classes and Objects

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

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

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS 302 Week 9. Jim Williams

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

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

AP CS Unit 7: Arrays Exercises

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

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Course Outline. Introduction to java

Full file at

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

CHAPTER 7 OBJECTS AND CLASSES

CS 251 Intermediate Programming Java Basics

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Abstract Classes and Interfaces

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS 2316 Exam 1 Practice ANSWER KEY

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

JAVA Programming Language Homework I - OO concept

CS 61B Discussion 5: Inheritance II Fall 2014

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

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

JAVA MOCK TEST JAVA MOCK TEST II

Building Java Programs. Chapter 1: Introduction to Java Programming

ASSIGNMENT 5 Objects, Files, and a Music Player

Software Practice 1 Basic Grammar

Week 3 Classes and Objects

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

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

Transcription:

CS 1331 Exam 1 Fall 2016 ANSWER KEY Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score. Signing signifies you are aware of and in accordance with the Academic Honor Code of Georgia Tech and that you will not discuss this exam with other students. Calculators and cell phones are NOT allowed. This is an object-oriented programming test. Java is the required language. Java is case-sensitive. DO NOT WRITE IN ALL CAPS. A Java program in all caps will not compile. Good variable names and style are required. Comments are not required. Question Points per Page Points Lost Points Earned Graded By Page 1 24 - = Page 2 10 - = Page 3 6 - = Page 4 4 - = Page 5 10 - = Page 6 10 - = Page 7 20 - = Page 8 0 - = TOTAL 84 - =

[16] 1. Evaluate each of the expressions below. Write down the value that they evaluate to, and the resulting data type of that value in the provided columns. If the expression is not valid Java syntax, or will throw an exception, simply write Error. Table 1: Expressions Expression Resulting Value of Expression Resulting Data Type (int)6.9 / 3 2 int (6 % 4 == 14 % 4) true boolean "cat"+ dog catdog String foo.equals(true? bar : foo ) false boolean "I have + 5 + dogs I have 5 dogs String "Pickle".substring(3,5) kl String "Pickle".charAt(5) e char "cat".indexof("a") 1 int 2. Legal or Not Legal If the following lines of code would not generate any errors when compiled, write LEGAL and the resulting values of all listed variables. If it will generate a compilation error, write NOT LEGAL and explain the problem. Assume all variables are local variables. [2] (a) int i = 3; double d; d = 7 * d * 3; NOT LEGAL d is undefined [2] (b) float f; double d; d = 16.0; f = (float) d / 2; LEGAL d = 16.0 f = 8.0f [2] (c) final int num = 99; double num2 = 99.3; num2 = num++; NOT LEGAL You are trying to change the value of num which is set as final. [2] (d) long g = 12; boolean b; b = 12 - g; NOT LEGAL incompatible types, a long cannot be converted to a boolean. Page 1 of 8 Points available: 24 - points lost: = points earned:. Graded by:

3. Short Answer [2] (a) Write the header for the method you need to define in a class to make it executable from the command line. public static void main(string[] args) [2] (b) Assume you are at the command line in the directory of the file that contains the definition for a Java class named Avengers. Write the command to compile Avengers. javac Avengers.java [2] (c) If the command above executes successfully, what file will be produced? Avengers.class [2] (d) Write the command that will execute the Avengers class you compiled above. java Avengers [2] (e) What will the following code print? for (int i = 2; i > 0; i--) System.out.print("Go "); System.out.print("Jackets!"); Go Go Jackets! Page 2 of 8 Points available: 10 - points lost: = points earned:. Graded by:

4. Multiple Choice Circle the letter of the correct choice. Given: public class Car { public String model = ""; public int year = 0; public Car(String amodel, int ayear) { model = amodel; year = ayear; public String tostring() { return "Model: " + model; public boolean equals(object other) { if (this == other) return true; if!(other instanceof Car) return false; if (this.model == ((Car) other).model) return true; return false; Assume the following statements have been executed: Car[] cars = { new Car("Camaro", 1967), new Car("Beetle", 1969), new Car("Jaguar", 2015) ; Car c = cars[1]; c.model = "VW Beetle"; [2] (a) What is the value of c? A. null B. automatically set to 0 C. cars[] D. the address of a Car object E. None of the above [2] (b) What is the value of cars.length? A. null B. the address of a Car object C. 2 D. 3 E. None of the above [2] (c) What is the value of the expression cars[1].equals(cars[2]))? A. true B. false C. This expression would cause an error to occur. Page 3 of 8 Points available: 6 - points lost: = points earned:. Graded by:

[2] (d) What is printed on the console by System.out.println(cars[1].toString());? A. Model: Beetle Year: 1969 B. Model: VW Beetle Year: 1969 C. Model: Camaro Year: 1967 D. Car: null E. None of the above [2] (e) What is printed on the console by System.out.println(cars);? A. null B. ["Model: ", "Model: ", "Model: "] C. ["Model: Camaro", "Model: Beetle", "Model: Jaguar"] D. Something like [LCar;@1db9742 Page 4 of 8 Points available: 4 - points lost: = points earned:. Graded by:

5. Tracing public class Tracer { public static void main(string[] args) { final String str = "fun 1331"; for (int i = 0; i < str.length(); i++) { if ((i < 5) (i > 2)) { System.out.print(str.charAt(i)); else { System.out.print(str.substring(i - 1, i)); if (i == 3) { System.out.println("Break time"); [5] (a) What is printed to the console when the program above is run? fun Break time 1331 class Tester { public static void main(string[] args) { String[] sa = new String[4]; double[] da = new double[3]; da[0] = 0; da[1] = 5; for (int i = 0; i < da.length; i++) { sa[i+1] = i + " : "; System.out.println(sa[i] + da[i]); [5] (b) What is printed when the program above is run? null0.0 0 : 5.0 1 : 0.0 Page 5 of 8 Points available: 10 - points lost: = points earned:. Graded by:

[10] 6. Class and ObjectTracing public class Poodle { private static int poodlecount = 0; private String name; public Poodle(String name) { this.name = name; poodlecount++; public void setname(string newname) { name = newname; public String getname() { return name; public String tostring() { return getname(); public String poodlecounter() { return "I am dog number " + poodlecount + "."; public static void poodlereport() { System.out.println("There are " + poodlecount + " poodles in this kennel."); public class PoodleTester { public static void main(string[] args) { Poodle curly = new Poodle("Curly"); System.out.println(curly.poodleCounter()); Poodle chloe = new Poodle("Chloe"); System.out.println(chloe.poodleCounter()); Poodle jenny = chloe; jenny.setname("jenny"); System.out.println(jenny.poodleCounter()); System.out.println(curly.poodleCounter()); Poodle.poodleReport(); What is printed when the PoodleTester is run? I am dog number 1. I am dog number 2. I am dog number 2. I am dog number 2. There are 2 dogs in this kennel. Page 6 of 8 Points available: 10 - points lost: = points earned:. Graded by:

7. Write the code. Be sure it would compile. [5] (a) Convert the following nested for loops to equivalent code that uses while loops instead instead of for loops. You do not need to rewrite the array declaration. String[][] arr = {{"Hello", "there", "world", {"cs 1331", "is the", "best"; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j]); System.out.println(); int i = 0; int j; while (i < arr.size) { j = 0; while (j < arr[i].size) { System.out.print(arr[i][j]); j++; System.out.println(); i++; [15] (b) The morning of your CS1331 test, you decide to make yourself a healthy breakfast. You throw some food in the toaster. This isn t just any old toaster, though. It loves music, and it ll only toast if you let it sing along. You must create an object in Java to represent your toaster: * It has a specific number of slots for toast and other food items. - Add an instance variable to store this value. - This value is set when the object is instantiated, and the value cannot be changed afterwards. * It has a specific song it likes to sing, which it remembers by name. - Add an instance variable to store the song name, and write methods to get and set this value. Be sure to use proper conventions for your getters and setters! * Write constructors that allow the above two values to be set when creating the object. - One constructor should allow both values to be set using input parameters. - One constructor should set the number of slots to the default value of 1 and the song name to Let It Be. * Write a toast method which takes in the name of an item to toast. - This method should print out a string containing the song the toaster is singing and the item being toasted. - For example, if the song to sing is Saint Pablo and you re toasting bread, the string you print out could be Singing Saint Pablo while I toast your bread!. Write your complete Toaster class on the next page following the class heading. You do not need to write a tester for your Toaster class. Page 7 of 8 Points available: 20 - points lost: = points earned:. Graded by:

public class Toaster { private final int numslots; // final keyword optional private String songtosing; public Toaster(int numslots) { this.numslots = 1; this.songtosing = "Let it Be"; public Toaster(int numslots, String songtosing) { this.numslots = numslots; this.songtosing = songtosing; public void toast(string item) { // print statement doesn t need to match exactly as long as it has the two variables. System.out.printf("Singing %s while I toast your %s.\n", songtosing, item); public String getsongtosing() { return this.songtosing; public void setsongtosing(string songtosing) { this.songtosing = songtosing; Page 8 of 8 Points available: 0 - points lost: = points earned:. Graded by: