CS 1331 Exam 1 Practice ANSWER KEY

Similar documents
Sample CSE8A midterm Multiple Choice (circle one)

Java Program Coding Standards Programming for Information Technology

Introduction to Java

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Moving from CS 61A Scheme to CS 61B Java

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CS 111 Classes I 1. Software Organization View to this point:

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

AP Computer Science Java Subset

Install Java Development Kit (JDK) 1.8

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CS 106 Introduction to Computer Science I

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Object-Oriented Programming

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Java CPD (I) Frans Coenen Department of Computer Science

CS170 Lab 11 Abstract Data Types & Objects

Example of a Java program

Part I. Multiple Choice Questions (2 points each):

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Java Interview Questions and Answers

Object Oriented Software Design

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Object Oriented Software Design

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Iteration CHAPTER 6. Topic Summary

Chapter 2 Introduction to Java programming

Java Crash Course Part I

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Crash Course in Java

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Chapter 1 Java Program Design and Development

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Basics of Java Programming Input and the Scanner class

Computer Programming I

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

C++ INTERVIEW QUESTIONS

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Java Basics: Data Types, Variables, and Loops

Programming Languages CIS 443

Installing Java (Windows) and Writing your First Program

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Building Java Programs

Lecture 5: Java Fundamentals III

Basic Programming and PC Skills: Basic Programming and PC Skills:

J a v a Quiz (Unit 3, Test 0 Practice)

Building Java Programs

Introduction to Programming System Design. CSCI 455x (4 Units)

Java Classes. GEEN163 Introduction to Computer Programming

CMSC 202H. ArrayList, Multidimensional Arrays

dictionary find definition word definition book index find relevant pages term list of page numbers

1001ICT Introduction To Programming Lecture Notes

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Java Programming Fundamentals

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CSE 308. Coding Conventions. Reference

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Basic Object-Oriented Programming in Java

Building a Multi-Threaded Web Server

The C Programming Language course syllabus associate level

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Chapter 2 Elementary Programming

Mail User Agent Project

Contents. 9-1 Copyright (c) N. Afshartous

Introduction to Java. CS 3: Computer Programming in Java

CS506 Web Design and Development Solved Online Quiz No. 01

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

java Features Version April 19, 2013 by Thorsten Kracht

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Beyond the Mouse A Short Course on Programming

Creating a Simple, Multithreaded Chat System with Java

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

Using ilove SharePoint Web Services Workflow Action

Graduate Assessment Test (Sample)

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Object-Oriented Programming in Java

cs2010: algorithms and data structures

More on Objects and Classes

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Building Java Programs

Week 2 Practical Objects and Turtles

Introduction to Programming

C Compiler Targeting the Java Virtual Machine

AP Computer Science A 2010 Scoring Guidelines

Web Development and Core Java Lab Manual V th Semester

Illustration 1: Diagram of program function and data flow

Transcription:

CS 1331 Exam 1 Practice ANSWER KEY Signing signifies you are aware of and in accordance with the Academic Honor Code of Georgia Tech. 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 9 - = Page 2 10 - = Page 3 10 - = Page 4 10 - = Page 5 20 - = Page 6 10 - = TOTAL 69 - =

1. True or False In each of the blanks below, write T if the statement beside the blank is true, F otherwise. [1] (a) F A public top-level Java class may be defined in a source file with any base name as long as the file extension is.java. [1] (b) F Java identifiers can contain letters, digits, and the underscore symbol and may start with a digit. [1] (c) F The statement int x = 3f/4f; will compile, but the result will be truncated so that x gets the value 0. [1] (d) F In a for loop header, for (initializer; condition; update), the Java compiler requires initializer to initialize a loop variable and update to update it. [1] (e) T The declarations double scores[] and double[] scores are equivalent. [1] (f) F Java arrays have a variable number of elements of mixed types. [1] (g) F Given an array named scores, the statement scores[scores.length + 1] will not compile. [1] (h) F Instance methods of a class can be called without first instaniating an object of the class. [1] (i) F Every Java class has a default no-arg constructor in addition to the constructors you write yourself. Page 1 of 6 Points available: 9 - points lost: = points earned:. Graded by:

2. Expression Evaluation For each expression below, write the value and then the Java data type of the evaluated legal expression in the space provided. Be exact. The type you give must be the exact spelling of a Java primitive type including uppercase vs lowercase as it would appear in your program. Expression: 7 / 2 [1] (a) Calculated value: 3 [1] (b) Java primitive type: int Expression: 64-16 * 2 [1] (c) Calculated value: 32 [1] (d) Java primitive type: int Expression: 2.5f + 3.0-1.5f [1] (e) Calculated value: 4.0 [1] (f) Java primitive type: double Expression: new Double(1) / 2 [1] (g) Calculated value: 0.5 [1] (h) Java primitive type: double Expression: true && "Foo".equals("foo") [1] (i) Calculated value: false [1] (j) Java primitive type: boolean Page 2 of 6 Points available: 10 - points lost: = points earned:. Graded by:

3. Multiple Choice Circle the letter of the correct choice. Given: public class Kitten { private String name = ""; public Kitten(String name) { name = name; public String tostring() { return "Kitten: " + name; public boolean equals(object other) { if (this == other) return true; if (null == other) return false; if (!(other instanceof Kitten)) return false; Kitten that = (Kitten) other; return this.name.equals(that.name); Assume the following statements have been executed: Kitten maggie = new Kitten("Maggie"); Kitten fiona = new Kitten("Fiona"); Kitten fiona2 = new Kitten("Fiona"); Kitten mittens = fiona; [2] (a) What is the value of maggie? A. the address of a Kitten object B. null C. automatically set to 0 D. undefined [2] (b) What is printed on the console after the following statement is executed? System.out.println(maggie.toString()); A. Kitten: B. Kitten: null C. Kitten: Maggie [2] (c) What is the value of the expression fiona == mittens)? A. true B. false [2] (d) What is the value of the expression fiona.equals(fiona2) && fiona == mittens? A. true B. false [2] (e) After executing Kitten[] kittens = new Kitten[5];, what is the value of kittens[0]? A. null B. the address of a Kitten object C. automatically set to 0 D. undefined Page 3 of 6 Points available: 10 - points lost: = points earned:. Graded by:

4. Tracing Consider the following code: public class StrangeLogic { private static int counter = 0; private static boolean incrementcounter() { counter++; return true; public static void main(string args[]) { boolean a = true, b = false; if (b incrementcounter()) { System.out.print("Boo"); if ((a b) && incrementcounter()) { System.out.print(" ya!"); System.out.println(counter); [5] (a) What is printed when main is executed? Solution: Boo ya! 2 ( 1 pt for Boo, 2 for ya!, 2 for 2) Consider the following code: public class AlGore { public static void main(string[] args) { String mystery = "mnerigpaba"; String solved = ""; int len = mystery.length(); for (int i = 0, j = len - 1; i < len/2; ++i, --j) { solved += mystery.charat(i) + mystery.charat(j); System.out.println(solved); [5] (b) What is printed when main is executed? Solution: manbearpig Page 4 of 6 Points available: 10 - points lost: = points earned:. Graded by:

5. Short Answer [2] (a) Assume you have a Java class named Foo that you want to be able to run from the command line. Write the header for the method you need to define in Foo to make it executable from the command line. Solution: 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 Foo. Write the command that you would execute on the command line to compile Foo. Solution: javac Foo.java [2] (c) If the command above executes successfully, what file will be produced? Solution: Foo.class [2] (d) Write the command that will execute the Foo class you compiled above. Solution: java Foo [3] (e) What will the following code print? for (int i = 10; i > 0; i--); System.out.print( Meow! ); Solution: Meow! [4] (f) Assume you have the following start of a Student class and Student constructor. Finish the constructor. Do not change the code that is given. public class Student { private String name; private String email; public Student(String name, String email) { // Your code goes here this.name = name; // 2 point this.email = email; // 2 point [5] (g) Convert the following for loop to an equivalent while loop. for (int i = 10; i > 0; i--) { System.out.println(i); Solution: int i = 10; while (i > 0) { System.out.println(i); i--; // 1 pts // 1 pt // 1 pt // 1 pt Page 5 of 6 Points available: 20 - points lost: = points earned:. Graded by:

6. Complete the Method [5] (a) Fill in the code for the following method that takes an array of numbers and returns the number of even numbers in the array argument. Your code should use a for loop. public int evens(int[] numbers) { // Your code goes here Solution: int count = 0; for (int number: numbers) { if ((number % 2) == 0) { count++; return count; [5] (b) Fill in the code for the following method that takes an array of numbers and a number and returns true if the array contains the number, false otherwise. You will need a loop, and your loop must not execute more iterations than necessary, and you cannot use break or continue. public boolean contains(int[] numbers, int n) { // Your code goes here Solution: boolean found = false; for initializations int i = 0; do { if (numbers[i] == n) { for conditional setting found = true; // of found to true i++; while (!found && (i < numbers.length)) for termination condition return found; for return Page 6 of 6 Points available: 10 - points lost: = points earned:. Graded by: