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

Similar documents
Lecture 19: Recursion

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Assignment 8B SOLUTIONS

Question: Total Points: Score:

Name CIS 201 Midterm II: Chapters 1-8

Chapter 7 User-Defined Methods. Chapter Objectives

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

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

Loops. CSE 114, Computer Science 1 Stony Brook University

CSE 143 Lecture 11. Decimal Numbers

CSE 143 SAMPLE MIDTERM

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

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

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

Building Java Programs

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CS 101 Spring 2007 Midterm 2 Name: ID:

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

Java Simple Data Types

Ch. 6. User-Defined Methods

Key Java Simple Data Types

H212 Introduction to Software Systems Honors

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Place your name tag here

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

C22a: Problem Solving using Recursion

University of Palestine. Mid Exam Total Grade: 100

Object Oriented Programming. Java-Lecture 1

Oct Decision Structures cont d

Loops and Expression Types

CSE 143 Lecture 10. Recursive Programming. reading: slides adapted from Marty Stepp and Hélène Martin

Data Structures COE 312 ExamII Preparation Exercises

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

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

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

CIS October 19, 2017

CS141 Programming Assignment #5

CSE 142 Sample Midterm Exam #3

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

CSE142 Sample Midterm, Winter 2018

Java Simple Data Types

Midterm Examination (MTA)

COMPUTER APPLICATIONS

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

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.

Arrays. Eng. Mohammed Abdualal

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

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

CMPS 11 Intermediate Programming Midterm 2 Review Problems

Programming with Java

CS 101 Exam 2 Spring Id Name

I. True/False: (2 points each)

Example: Monte Carlo Simulation 1

b. Suppose you enter input from the console, when you run the program. What is the output?

Computer Science is...

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CSE 142 Sample Midterm Exam #3

CS170 Introduction to Computer Science Midterm 2

COS 126 General Computer Science Fall Written Exam 1

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

AP COMPUTER SCIENCE A

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Write a program which converts all lowercase letter in a sentence to uppercase.

Practice Midterm 1. Problem Points Score TOTAL 50

Netbeans tutorial:

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Selenium Class 9 - Java Operators

(a) Assume that in a certain country, tax is payable at the following rates:

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

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Practice Problems: Instance methods

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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)

Fundamentals of Programming Data Types & Methods

Practice Problems: Object & instance methods

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Example Program. public class ComputeArea {

CMPS 12A Introduction to Programming Midterm 2 Review Problems

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Selected Questions from by Nageshwara Rao

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

ICSE Class 10 Computer Applications ( Java ) 2010 Solved Question...

Object Oriented Programming. Java-Lecture 6 - Arrays

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CSE 143 SAMPLE MIDTERM SOLUTION

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

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

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

( &% class MyClass { }

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013


CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CIS March 1, 2018

Transcription:

CS1 Midterm 1. Read the following code fragments and answer the questions. (a) public void displayabsx(int x) { if (x > 0) { System.out.println(x); return; else { System.out.println(-x); return; System.out.println("Done"); What is the flaw that stops the above method from compiling? Answer: The last line of the code cannot be reached since both the if and else clauses return from the method. (b) class X { public int meth(int a, int b) {... public String meth(int a, int b) {... Does the above code fragment compile? Explain. Answer: These two overloaded methods use identical parameters. This is illegal. Therefore, the program will not compile. (c) public boolean hasduplicatevalues(int[] data) { for(int i = 0; i < data.length; i++) for(int j = 0; j < data.length; j++) if(data[i] == data[j]) return true; return false; Does the above method work properly? If it doesn t, fix it. Answer: The code fragement will give an incorrect result if the there are no duplicate values in the data. Since data[0] == data[0], the first comparison already returns true!. This error can be remedied by replacing j = 0 by j = i + 1. This way, only actual duplicates will be found.

(d) String names = { "George", "susan" ; int totallength = 0; for (int i = 0; i < names.length(); i++) totallength += names[i].length; Find the error in the above code fragment. Answer: This code fragement has three errors. The first is the missing square brackets in the array declaration. String[] names = \{"George", "Susan"\; //corrected statement The second error: the length of the array is given by names.length. It s not a method. So, it doesn t have attached empty parentheses. The third error: totallength is the sum of the length of the two String elements of the array names. The length here is a method. So it should be written as names[i].length(); (e) public class Oops { private int x = 3; public static void changex() { x = 4; What is the error in the above code fragment? Answer: An instance variable can t be accessed from a static method in the same class without an object and an accessor method. The method changex() shouldn t be a static method.. Please complete each part before moving to the next. (a) Create a class Android whose objects have unique names.the class has the following attributes: tag - a static integer that begins at 1 and changes each time an instance is created. name - a string that unique for each instance of this class. Create the class and declare the instance variables. public class Android { private static int tag = 1; private String name; Page

(b) Write a default constructor that changes the tag to the next prime number by calling the private method changetag(), and then sets the name to Bob concatenated with the value of tag. Answer: public Android() { changetag(); name = "Bob" + tag; (c) Write a getname() method that returns the name of the Android object. public String getname(){ return name; (d) Write a private static method isprime(n) that returns true if n is prime - that is, if it is not divisible by any number from to n-1. private static boolean isprime(int n){ for(int i = ; i < n; i++) if(n % i == 0) return false; return true; (e) Write a private static method changetag() that replaces tag with the next prime number larger than the current value of tag. private static int changetag() { tag++; if(!isprime(tag)) tag++; return tag; Page 3

3. Here is a code for a recursive method mystery. What is printed when mystery(,) is called? public class TestMystery { public static void main(string[] args) { mystery(,); public static void mystery(int a, int b) { if( a == 0 && b == 0) System.out.println(0); else if ( a == 0) { //System.out.println(a, b-1); //This line has error System.out.println(b); //This is the correct statement mystery(a, b-1); else { mystery(a-1, b); System.out.println(b); Write the output in the order it will be printed when the program is run on the computer. The output from the program is: 1 0 When the method is first called, it enters the else branch, where the function is recursively called each time with a smaller value of a. However, output doesn t get printed here because the print statement is after the recursive call. When a becomes 0, then program enters the else if block and prints b before recursively calling the function with the smaller value of b. The first output is from this block followed by the output from the if block when both a and b are zero and finally the output from the else block is printed. N ote: Every one was given 4 points for this question due to the error in the question. Page 4

4. A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I ere I saw Elba Desserts I stressed kayak Write a complete java program that uses recursion to determine whether a string argument is a palindrome. The program should have a main method and a static recursive ispalindrome method. import java.util.scanner; public class Palindrome { public static void main(string[] args){ String str; Scanner input = new Scanner(System.in); System.out.println("Enter a string to test if it is a palindrome: "); str = input.nextline(); //At this point, if there are commas etc in the text one can remove // them at this time. Even spaces can be removed. str = str.tolowercase(); if (ispalindrome(str)) System.out.println("The string, " + str + ", is a palindrome"); else System.out.println("The string, " + str + ", is not a palindrome"); input.close(); public static boolean ispalindrome(string s) { int len = s.length(); if (len <= 1) return true; return (s.charat(0) == s.charat(len-1)) && ispalindrome(s.substring(1, (len-1))); Page 5