CS 139 Practice Midterm Questions #2

Similar documents
MIDTERM EXAMINATION - CS130 - Spring 2003

CS 251 Intermediate Programming Methods and More

Expression: Expression: Statement: Output: Expression: Expression:

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

APCS Semester #1 Final Exam Practice Problems

CS 251 Intermediate Programming Methods and Classes

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

Introduction to Programming Using Java (98-388)

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

CS302: Self Check Quiz 2

Building Java Programs

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

CS111: PROGRAMMING LANGUAGE II

Review for Test 1 (Chapter 1-5)

Computational Expression

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

CS 159 Midterm #1 Review Questions

CS159. Nathan Sprague

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Getting started with Java

CS159 Midterm #1 Review

COS 126 General Computer Science Spring Written Exam 1

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Practice Midterm 1. Problem Points Score TOTAL 50

CSC 1351: Quiz 6: Sort and Search

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Key Java Simple Data Types

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Programming for Engineers Functions

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Flow Control. CSC215 Lecture

Name CIS 201 Midterm II: Chapters 1-8

CS313D: ADVANCED PROGRAMMING LANGUAGE

Expressions and Casting

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Array. Prepared By - Rifat Shahriyar

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Java Simple Data Types

Selected Questions from by Nageshwara Rao

Problem Score Max Score 1 Syntax directed translation & type

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Midterm Examination (MTA)

Decision Making in C

Chapter 2. Section 2.3 if and if-else statements. CS 50 Hathairat Rattanasook

Fundamentals of Programming Data Types & Methods

CS111: PROGRAMMING LANGUAGE II

1 Lexical Considerations

Section 2.2 Your First Program in Java: Printing a Line of Text

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

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

Pointers and scanf() Steven R. Bagley

Flow of Control. Chapter 3 Part 3 The Switch Statement

2.8. Decision Making: Equality and Relational Operators

FORM 2 (Please put your name and form # on the scantron!!!!)

Primitive Data, Variables, and Expressions; Simple Conditional Execution

The SPL Programming Language Reference Manual

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Pace University. Fundamental Concepts of CS121 1

Building Java Programs

This is CS50. Harvard University Fall Quiz 0 Answer Key

Introduction to C Programming

Topic 4 Expressions and variables

COSC2031 Software Tools Introduction to C

Give one example where you might wish to use a three dimensional array

CHAPTER 7 OBJECTS AND CLASSES

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

Building Java Programs

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Midterm Exam CS 251, Intermediate Programming October 8, 2014

String Instructions In C Program Examples >>>CLICK HERE<<<

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Object-Oriented Programming

CS 231 Data Structures and Algorithms, Fall 2016

Array Initialization

Final CSE 131B Spring 2004

SECTION II: LANGUAGE BASICS

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

Examination Questions Midterm 1

CSE 11 Midterm Fall 2008

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

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 4 Introduction to Control Statements

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

An overview of Java, Data types and variables

Question Points Score

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Transcription:

CS 139 Practice Midterm Questions #2 Spring 2016 Name: 1. Write Java statements to accomplish each of the following. (a) Declares numbers to be an array of int s. (b) Initializes numbers to contain a reference to a new integer array of size 7. (c) Stores the value 3 in the last position of the array numbers. (Your answer should not use a literal value for the array index.) 2. Evaluate the following Java expressions or indicate the syntax errors they contain: (a) true true (b) true && False (c) true && (true true) (d) false && (true false) (e) NOT (true (true or false)) (f) (true false) && (true false) (g) (true false) && (5 > 7) (h) (5 < 7) && (5 > 7) (i) 5 < "7" (j) A < C

CS 139 Practice #2 Page 2 of 9 3. What would be printed by the following application (if it were compiled and executed properly)? public class Shapes { public static void main ( String [] args ) { System. out. println ("R1: " + issquare (300, 10)); System. out. println ("R2: " + issquare (20, 400)); System. out. println ("R3: " + issquare (20, 20)); public static boolean issquare ( int width, int length ) { return ( width == length ); 4. What would be printed by the following application (if it were compiled and executed properly)? Is iscircle a good name for this method? public class Shapes { public static void main ( String [] args ) { System. out. printf ("C1: %s\n", iscircle (300, 10)); System. out. printf ("C2: %s\n", iscircle (20, 400)); System. out. printf ("C3: %s\n", iscircle (20, 20)); public static boolean iscircle ( int width, int length ) { boolean result ; if ( width == length ) { result = true ; else { result = false ; return result ; Page 2

CS 139 Practice #2 Page 3 of 9 5. What would be printed by the following application (if it were compiled and executed properly)? public class Worthless { public static void main ( String [] args ) { int age, answer, areacode, iq, zipcode ; age = 67; areacode = 540; iq = 289; zipcode = 22801; answer = useless ( age, areacode, iq, zipcode ); System. out. println ("ID: " + answer ); public static int useless ( int a, int b, int c, int d) { int answer ; answer = 0; if (d > 65) { answer = 1; if (b == 540) { answer = 2; else { if (c > 200) { answer = 5; return answer ; 6. For the question above, number the statements in the order in which they would be completed. Page 3

CS 139 Practice #2 Page 4 of 9 7. Consider the following fragment: boolean cangraduate ; if (( credits >= 120) && ( gpa >= 2.0)) { cangraduate = true ; else { cangraduate = false ; Rewrite this fragment: (a) Using nested if statements with a single relational operator in each. (b) Without an else block. (c) Reversing the roles of the if and else blocks (i.e., the if block should assign false to cangraduate). (d) Without an if statement (i.e., using just assignment statements). 8. What would be printed by the following fragment (if it were compiled and executed properly)? for ( int k = 5; k > 0; k = k - 1) { System. out. printf ("T minus %d\n", k); 9. Rewrite the for loop above as a while loop. 10. Rewrite it as a do-while loop. Page 4

CS 139 Practice #2 Page 5 of 9 11. What would be printed by the following fragment (if it were compiled and executed properly)? int i; int n; n = 0; for (i = 0; i <= 5; n ++) { System. out. printf ("%d", i); 12. What would be printed by the following fragment (if it were compiled and executed properly)? int i; i = 0; while (i < 2) { System. out. print ("+"); i ++; while (i < 2) { System. out. print ("-"); i ++; 13. What would be printed by the following fragment (if it were compiled and executed properly)? for ( int i = 0; i < 4; i ++) { for ( int j = 2; j >= i; j - -) { System. out. printf ("%d %d\n", i, j); Page 5

CS 139 Practice #2 Page 6 of 9 14. Consider the code fragment below: Die [] dice1 ; Die [] dice2 ; dice1 = new Die [4]; dice2 = dice1 ; dice1 [0] = new Die (1); for ( int i = 0; i < dice1. length ; i ++) { dice2 [ i] = dice1 [0]; dice1 [1] = new Die (3); for ( Die curdie : dice2 ) { System. out. println ( curdie. getface ()); (a) How many array variables are declared in this code? (b) How many arrays are created in this code? (Remember that arrays are created using the new keyword.) (c) How many Die objects are created in this code? (d) Draw a diagram illustrating the contents of memory after this fragment executes. (e) What output will be produced by this code? Page 6

CS 139 Practice #2 Page 7 of 9 15. Complete the following method (using a loop): / This method calculates the probability of tossing a coin n times and and getting n heads The probability of getting a single head is 0.5. The probability of getting two heads is 0. 5 0. 5. The probability of n heads is 0.50.5...0.5 ( where there are n terms in the product ). @param n The number of tosses @return The probability of n heads in n tosses / public static double probabilityofheads ( int n) { 16. Complete the following method (using a loop). / This method finds the number of parentheses in a String @param text The String @return The number of parentheses / public static int numberofparentheses ( String text ) { Page 7

CS 139 Practice #2 Page 8 of 9 17. Complete the following method. Use an enhanced for loop. / This method returns true if the target string appears at least twice in the provided array of strings. All comparisons are case - sensitive. @param strings - The array of Strings that must be searched @param target - The String to search for. @return true if the target appears at least twice, false otherwise / public static boolean appearstwice ( String [] strings, String target ) { 18. Complete the following method. / This method takes an integer array as an argument and returns a copy of that array in which all negative values have been replaced by 0. THE ORIGINAL ARRAY MUST NOT BE MODIFIED. @param data The array to copy @return The new array ( with no negative values ) / public static int [] nonnegative ( int [] data ) { Page 8

CS 139 Practice #2 Page 9 of 9 19. Complete the following method. / This method takes an array of strings that may have some null elements. It returns a new ( possibly smaller ) array with the null elements removed. For example if the input array were {" A", null, " B", the return value would be {"A", "B ". THE ORIGINAL ARRAY MUST NOT BE MODIFIED. @param text The array of strings ( possibly containing some null entries.) @return A copy of the array without any null values. / public static String [] removenulls ( String [] text ) { // I suggest the following steps : // // 1. Use a loop to count the non - null entries. ( This could be made into a // helper method.) // 2. Create a new array large enough to hold the non - null entries. // 3. Loop through the original array, copying the non - null entries into // the new array. Page 9