CMPS 12A Introduction to Programming Final Exam Review Problems

Similar documents
Homework/Program #5 Solutions

Sample CSE8A midterm Multiple Choice (circle one)

Using Files as Input/Output in Java 5.0 Applications

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

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Introduction to Java

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

Programming Languages CIS 443

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

(Eng. Hayam Reda Seireg) Sheet Java

Object-Oriented Programming in Java

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

13 File Output and Input

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

CS 121 Intro to Programming:Java - Lecture 11 Announcements

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

AP Computer Science Java Subset

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

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

public static void main(string[] args) { System.out.println("hello, world"); } }

Section 6 Spring 2013

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

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

Java CPD (I) Frans Coenen Department of Computer Science

Using Two-Dimensional Arrays

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

Crash Course in Java

AP Computer Science Java Mr. Clausen Program 9A, 9B

Continuous Integration Part 2

Object Oriented Software Design

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

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

Course Intro Instructor Intro Java Intro, Continued

WRITING DATA TO A BINARY FILE

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

AP Computer Science File Input with Scanner

Object Oriented Software Design

Basics of Java Programming Input and the Scanner class

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

CS170 Lab 11 Abstract Data Types & Objects

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

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Simple Java I/O. Streams

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

Introduction to Programming

Arrays in Java. Working with Arrays

C++ INTERVIEW QUESTIONS

JAVA ARRAY EXAMPLE PDF

Explain the relationship between a class and an object. Which is general and which is specific?

Application Development with TCP/IP. Brian S. Mitchell Drexel University

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

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

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

Building a Multi-Threaded Web Server

3 length + 23 size = 2

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

JAVA.UTIL.SCANNER CLASS

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

Creating a Simple, Multithreaded Chat System with Java

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

How to Install Java onto your system

Chapter 2 Introduction to Java programming

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

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

java Features Version April 19, 2013 by Thorsten Kracht

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Inside the Java Virtual Machine

D06 PROGRAMMING with JAVA

Lecture J - Exceptions

More on Objects and Classes

Arrays. Introduction. Chapter 7

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

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

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

Reading Input From A File

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

CSE 8B Midterm Fall 2015

Java Interview Questions and Answers

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

Software Development in Java

Files and input/output streams

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

The following program is aiming to extract from a simple text file an analysis of the content such as:

AP Computer Science Static Methods, Strings, User Input

cs2010: algorithms and data structures

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

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

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Chapter 7: Sequential Data Structures

Introduction to Java. CS 3: Computer Programming in Java

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

Chapter 3. Input and output. 3.1 The System class

Visit us at

Transcription:

CMPS 12A Introduction to Programming Final Exam Review Problems 1. Determine the output of the following Java program. // Problem1.java import java.util.scanner; import java.io.*; class Problem1{ public static void main( String[] args ){ int a=5, b=6, c=1; double x=0.5, y=1.0, z=1.5; c = fcn1(a, b); y = fcn2(y, a); b = fcn3(x, y); z = fcn3(c, b); System.out.println("a="+a+", b="+b+", c="+c); System.out.println("x="+x+", y="+y+", z="+z); static int fcn1(int i, int j){ int k = i-j; return (++k); static double fcn2(double t, int n){ return (t*n); static int fcn3(double u, double v){ return fcn1((int)(u*v), 2); static double fcn3(int r, int s){ return fcn2(r,s);

2. Complete the static method getmax() in the following Java program in such a way that it returns the maximum value stored in its 2-dimensional array argument. Note that the array passed to getmax() need not be rectangular, i.e. its rows may have differing lengths. // Problem2.java class Problem2{ int[][] table = { {3, 9, 6, 12, {23, -25, 54, {0, -12, 27, 8, 16 ; System.out.println(getMax(table)); // prints 54 static int getmax(int[][] A){ // your code goes here 3. Determine the output of the following Java program. Assume that the program is run four times with the command line arguments file1, file2, file3, and file4, and assume that these files contain the following characters: file1: zero 3 2 4 17 5 8 9 16 13 28 77 12 file2: one 4 3 72 0 9 2 17 5 19 50 8 91 14 file3: two 5 3 9 2 18 27 45 66 91 92 93 file4: blah 2 0 1 14 101 83 17 74 6 89 234 Print the program output only, do not print the Unix commands that invoke the program. // Problem3.java import java.util.scanner; import java.io.*; class Problem3{ public static void main(string[] args) throws FileNotFoundException{ Scanner sc = new Scanner(new File(args[0])); String str = sc.next(); int n; while(sc.hasnextint()){ n = sc.nextint(); if ( str.equals("zero") && n%3==0 ) System.out.print(n + " "); else if( str.equals("one") && n%3==1 ) System.out.print(n + " "); else if( str.equals("two") && n%3==2 ) System.out.print(n + " "); else continue; System.out.println("end of " + str + "s");

4. Determine the output of the following Java program. (Hint: figure out what each part of the program does, particularly the methods Mix() and Print(), then put the pieces together.) Assume the program is invoked with one command line argument: % java Problem4 myfile, where myfile is a file containing the single line of text: one two three four five six seven eight nine ten // Problem4.java import java.util.scanner; import java.io.*; class Problem4{ public static void main(string[] args) throws FileNotFoundException { Scanner sc = new Scanner(new File(args[0])); String[] Z = new String[10]; int[] A = {2, 1, 9, 8, 7, 6, 4, 5, 3, 0; for(int i=0; i<10; i++) Z[i] = sc.next(); Mix(Z, A); Print(Z); static void Mix(String[] S, int[] I){ int j, n = S.length; String[] temp = new String[n]; for(j=0; j<n; j++) temp[j] = S[I[j]]; for(j=0; j<n; j++) S[j] = temp[j]; static void Print(String[] S){ for(int j=0; j<s.length; j++) System.out.println(S[j]);

5. Determine what is printed by the following Java program, which consists of two files: Place.java and PlaceTest.java. Assume that both files are in the same directory. // Place.java class Place{ // Fields String name; double latitude; double longitude; // Constructor Place(String n, double lat, double lon){ name = n; latitude = lat; longitude = lon; public String tostring(){ return( name +": "+latitude+" N "+longitude+" W" ); boolean isfurtherwestthan(place x){ return(this.longitude>x.longitude); boolean isfurthernorththan(place x){ return(this.latitude>x.latitude ); // PlaceTest.java class PlaceTest{ Place[] P = new Place[4]; P[0] = new Place("Doriath", 33.1, 84.5); P[1] = new Place("Nargothrond", 39.8, 101.7); P[2] = new Place("Thangorodrim", 45.0, 73.3); P[3] = new Place("Gondolin", 40.6, 74.2); int i, n; for(n=p.length; n>1; n--) for(i=0; i<n-1; i++) if( P[i].isFurtherNorthThan(P[i+1]) ) swap(p, i, i+1); printplacearray(p); static void swap(place[] A, int i, int j){ Place temp = A[i]; A[i] = A[j]; A[j] = temp; static void printplacearray(place[] A){ for(int i=0; i<a.length; i++) System.out.println(A[i]);

6. The Java program below consists of two files: Rational.java and RationalTest.java, assumed to be in the same directory. The Rational class represents rational numbers as a pair of integers. Complete methods plus(), times() tostring(), and equals() in Rational.java so that the output is: 7/5+13/3 = 86/15 7/5*13/3 = 91/15 7/5*(13/3+28/20) = 2408/300 7/5 is not equal to 13/3 7/5 is equal to 28/20 // RationalTest.java class RationalTest{ String str; Rational x = new Rational(7, 5); Rational y = new Rational(13, 3); Rational z = new Rational(28, 20); System.out.println(x +"+"+ y +" = "+ x.plus(y) ); System.out.println(x +"*"+ y +" = "+ x.times(y) ); System.out.println(x +"*("+ y +"+"+ z +") = "+ x.times(y.plus(z))); str = (x.equals(y)?" is equal to ":" is not equal to "); System.out.println(x + str + y); str = (x.equals(z)?" is equal to ":" is not equal to "); System.out.println(x + str + z); // Rational.java class Rational{ int numerator; int denominator; // Fields Rational(int n, int d){ // Constructor if(d==0) throw new RuntimeException("zero denominator"); numerator = n; denominator = d; Rational plus(rational Q){ Rational times(rational Q){ public String tostring(){ public boolean equals(object x){ // end of class Rational

7. Complete the following java program which scans a file containing only numbers and white space, and prints out the average value of all the numbers in the file. For instance, given a file called test containing the numbers: 2.4 3.4 5.8 6.0 7-3 12.2 8, and given that your program is called by doing % java Problem7 test at the command line, your program will produce the following output. The average value in file test is 5.225 Your program will check that the number of command line arguments is equal to 1, and will initialize a Scanner object (in an appropriate try-catch block) pointing to the file named on the command line. If any of these tests fail, your program will exit with an error message. You may assume that the file will contain at least one number. // Problem7.java import java.util.scanner; import java.io.*; class Problem7{ // your begins here // your code ends here

8. Determine the output of the following Java program consisting of the files: Itype.java, Atype.java, Btype.java and ItypeTest.java. Assume that all files are in the same directory. // Itype.java interface Itype{ void foo(); // Atype.java class Atype implements Itype{ public void foo(){ System.out.println("in Atype's foo()"); public String tostring(){ return "I am an Atype object"; // Btype.java class Btype extends Atype{ Btype(){ System.out.println("constructing a Btype object"); public void foo(){ System.out.println("in Btype's foo()"); public void bar(){ System.out.println("in Bytpe's bar()"); public String tostring(){ return "I am a Btype object"; // ItypeTest.java class ItypeTest{ Itype I; Atype A; Btype B; A = new Atype(); B = new Btype(); I = A; A = B; I.foo(); A.foo(); B.bar(); System.out.println(I); System.out.println(A); System.out.println(B);

9. Write a complete Java program called EchoWords.java that reads a series of words (tokens) from standard input, echoing each word to standard out on its own line until the word "galumph" is encountered. At that point the program prints "bye!" and terminates. One possible run of the program would be: % java EchoWords one two three four galumph one two three four bye! 10. Write a complete Java program called DrawSquare.java that reads a positive integer n from the command line, where n is at least 2, then prints out an n by n square made of asterisks * to standard out. Your program will print a usage message (see below) and quit if the number of command line arguments is not exactly one, if that argument cannot be parsed as an integer, and if that integer is not at least two. Several runs of the program are printed below. % java DrawSquare Usage: java DrawSquare <positive integer at least 2> % java DrawSquare 1 Usage: java DrawSquare <positive integer at least 2> % java DrawSquare 2 ** ** % java DrawSquare 3 *** *** % java DrawSquare 5 ***** ***** $ java DrawSquare 10 ********** **********

11. Write a class called DownCounter that extends the abstract class AbstractCounter which was discussed in class (and which is still posted on the course website.) Class DownCounter will define the abstract method click() in such a way as to count down from limit to 1. Thus DownCounter's constructor will set value equal to limit. When click() has been called limit times, value will be set back to limit (instead of 0). Class DownCounter will also override the inherited method reset() so as to set value back to limit (instead of 0). Place your class in a file called DownCounter.java, and place this file in a directory together with AbstractCounter.java and the program DownCounterTest.java printed below. Compile and run this program and check that the output is correct (see below). class DownCounterTest{ public static void main( String[] args ){ DownCounter a = new DownCounter(5); DownCounter b = new DownCounter(7); for(int i=0; i<11; i++){ System.out.println(a+" "+b); a.click(); b.click(); System.out.println(a+" "+b); clickandshow(a); resetandshow(a); // clickandshow() // calls click() then prints c static void clickandshow( AbstractCounter c ){ c.click(); System.out.println(c); // resetandshow() // calls reset() then prints c static void resetandshow( AbstractCounter c ){ c.reset(); System.out.println(c); /* Program Output: 5 7 4 6 3 5 2 4 1 3 5 2 4 1 3 7 2 6 1 5 5 4 4 3 3 5 */