CMPS 11 Intermediate Programming Midterm 2 Review Problems

Similar documents
CMPS 12A Introduction to Programming Midterm 2 Review Problems

CMPS 11 Intermediate Programming Final Exam Review Problems

CMPS 12A Fall 2017 Final (A)

University of Palestine. Mid Exam Total Grade: 100

CMPS12A Practice Final Fall 2017

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

JAVA OPERATORS GENERAL

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

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CMP 326 Midterm Fall 2015

Selenium Class 9 - Java Operators

CMPS 11 Introduction to Programming Midterm 1 Review Problems

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Introduction to Programming (CS112): Sample

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

Prof. Navrati Saxena TA: Rochak Sachan

ESC101 : Fundamental of Computing

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

H212 Introduction to Software Systems Honors

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

coe318 Lab 2 ComplexNumber objects

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

CS180. Exam 1 Review

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

University of Massachusetts Amherst, Electrical and Computer Engineering

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

Java Programming for Selenium

CS 231 Data Structures and Algorithms, Fall 2016

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

CIS 120 Programming Languages and Techniques. Midterm II. November 12, 2010

More types, Methods, Conditionals. ARCS Lab.

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

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

Java Simple Data Types

Operators Questions

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

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

CS 201, Fall 2016 Sep 28th Exam 1

CS 101 Spring 2007 Midterm 2 Name: ID:

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

CIS 120 Programming Languages and Techniques. Midterm II. November 12, Answer key

Computational Expression

CS 101 Exam 2 Spring Id Name

AP COMPUTER SCIENCE A

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

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

Chapter 4 Defining Classes I

Programming Problems 22nd Annual Computer Science Programming Contest

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Place your name tag here

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Key Java Simple Data Types

Operators and Expressions

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

CLASS DESIGN. Objectives MODULE 4

Java Simple Data Types

CMSC 150 LECTURE 7 RECURSION

Visual Programming. Lecture 2: More types, Methods, Conditionals

Final Exam Practice Questions

Introduction to Programming Written Examination

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

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

JAVA Programming Language Homework II Student ID: Name:

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

Lecture 5: Methods CS2301

I. True/False: (2 points each)

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

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

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

CSC 1351 The Twelve Hour Exam From Hell

Name CIS 201 Midterm II: Chapters 1-8

Introduction to Programming Using Java (98-388)

Polymorphism. return a.doublevalue() + b.doublevalue();

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Loops. CSE 114, Computer Science 1 Stony Brook University

NATIONAL UNIVERSITY OF SINGAPORE

Lara Technologies Special-Six Test

Solutions to Sample JAC444 Midterm Test

CS 312 Midterm 1 Spring 2013

CSE 11 Midterm Fall 2012

Methods. Eng. Mohammed Abdualal

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Problem Grade Total

Research Group. 2: More types, Methods, Conditionals

COMPUTER APPLICATIONS

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

Day 2 : Intermediate Concepts 1 Examples

Chapter 10: Recursive Problem Solving

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

AP CS Unit 6: Inheritance Notes

Transcription:

CMPS 11 Intermediate Programming Midterm 2 Review Problems 1. Determine the output of the following Java program. Notice that the method fcn2() is overloaded, so there are really three distinct functions here. // Problem1.java class Problem1{ public static void main( String[] args ){ int a=5, b=3, c; double x=2.2, y=1.3, z; c = fcn1(a, b); b = fcn2(x, y); z = fcn2(c, b); System.out.println("a="+a+", b="+b+", c="+c); System.out.println("x="+x+", y="+y+", z="+z); System.out.println("bye!"); static int fcn1(int i, int j){ int k = (i++) + (++j); return k+2; static int fcn2(double u, double v){ return fcn1((int)(u+v), 3); static double fcn2(int r, int s){ return (double)fcn1(r+s, 3); 2. Fill in the definition of function concatenate() below. This function takes as input two int arrays A and B, of any length, and returns a new int array which is the concatenation of A and B, i.e. the length of the returned array is the sum of the lengths of A and B, and the contents of the returned array are the values in A followed by the values in B. static int[] concatenate(int[] A, int[] B){ // your code ends here

3. Determine the output of the following Java program. Note that the function f() is recursive. // Problem3.java class Problem3{ public static void main(string[] args) { System.out.println(f(7)); static int f(int n){ System.out.println( "f(" + n + ")" ); if(n<=1) return 2; else return f(n-1) + 5; 4. Write a recursive function called sum() that takes a single int n as input and returns the sum of the positive integers from 1 to n. (Hint: this is the same as the recursive function that computes n! (n factorial) discussed in class, except with multiplication replaced by addition.) static int sum(int n){.... 5. Determine the output of the following Java program. (Hint: first figure out what each of the methods f(), g() and h() do, then put the pieces together.) // Problem5.java class Problem5{ int[][] A = {{2, 4,{5, -1; int[][] B = {{5, 3,{-3, 2; System.out.println(f(A)); h(b); System.out.println(f(g(B))); h(g(a)); static int f(int[][] M){ return M[0][0]*M[1][1]-M[0][1]*M[1][0]; static int[][] g(int[][] M){ int[][] N = new int[2][2]; for(int i=0; i<2; i++) for(int j=0; j<2; j++) N[i][j] = M[j][i]; return N; static void h(int[][] M){ for(int i=0; i<2; i++){ for(int j=0; j<2; j++) System.out.print(M[i][j]+" "); System.out.println();

6. Determine the output of the following Java program. This program consists of two files Blah.java and BlahTest.java defining the classes Blah and BlahTest respectively. // Blah.java class Blah{ private int foo; private double bar; Blah(int f, double b){ foo = f; bar = b; Blah(int a, int b){ foo = a+b; bar = 0.0; int getfoo(){ return foo; void setbar(double x){ bar = x; void mult(){ bar *= foo; public String tostring(){ return "("+foo+", "+bar+")"; public boolean equals(object x){ boolean eq = false; Blah B; if( x instanceof Blah ){ B = (Blah)x; eq = (foo==b.foo && bar==b.bar); return eq; // BlahTest.java class BlahTest{ Blah A = new Blah(15, 3.0); Blah B = new Blah(7, 8); System.out.println(A); System.out.println(B); A.mult(); B.setBar(45.0); System.out.println(A.equals(B));

7. Complete the methods getmaxindex() and getminindex() in the Java program below. These function should return the index at which the maximum (respectively minimum) value of its array argument is stored. Functions getmaxindex() and getminindex() should work on any int array of any length, not just the one specified in function main() below. // Problem7.java class Problem7{ int [] list = {3, 9, 6, 12, 23, -25, 54, 9, 0, -12, 27; System.out.println(list[getMaxIndex(list)]); // prints 54 System.out.println(list[getMinIndex(list)]); // prints -25 static int getmaxindex(int[] A){ // your code ends here static int getminindex(int[] A){ // your code ends here

8. Determine the output of the following Java program. This program consists of two files: Point.java which defines the Point class, and PointTest.java which defines the PointTest class and contains function main(). // Point.java class Point{ // Fields private int xcoord; private int ycoord; // Constructor Point(int x, int y){ xcoord = x; ycoord = y; // Methods public String tostring(){ return "(" + xcoord + ", " + ycoord + ")"; void reflect(){ int temp = xcoord; xcoord = ycoord; ycoord = temp; boolean isleftof(point P){ return (this.xcoord < P.xcoord); boolean isbelow(point P){ return (this.ycoord < P.ycoord); // PointTest.java class PointTest{ public static void main( String[] args ){ Point A = new Point(1, 5); Point B = new Point(2, -3); Point C = new Point(4, 3); Point D = new Point(8, 7); String str1, str2; A.reflect(); D.reflect(); System.out.println("A = " + A); System.out.println("B = " + B); System.out.println("C = " + C); System.out.println("D = " + D); str1 = A.isLeftOf(B)?"left":"right"; str2 = C.isBelow(D)?"below":"above"; System.out.println("A is to the " + str1 + " of B"); System.out.println("C is " + str2 + " D"); 9. Write a Java class called Distance that encapsulates a single data field of type double representing a distance measured in Meters. Write a single constructor for the class that takes a double argument and copies its value to the data field. Write three access functions called getmeters(), getfeet() and getfurlongs() that return respectively: the value stored in the data field, the distance measured in feet and the distance measured in furlongs. (Google "feet per meter" and "furlongs per meter" to learn the relevant conversion factors.)