CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Similar documents
CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2017

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CMSC 202 Midterm Exam 1 Fall 2015

I. True/False: (2 points each)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

Introduction to Computer Science Midterm 3 Fall, Points

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

I. True/False: (2 points each)

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Make sure the version number is marked on your scantron sheet. This is Version 1

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

ESC101N: Fundamentals of Computing End-sem st semester

Approximately a Final Exam CPSC 206

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

More Arrays. Last updated 2/6/19

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CSCE Practice Midterm. Data Types

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

tablica: array: dane_liczbowe

C: How to Program. Week /Mar/05

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

I. True/False: (2 points each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

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

Exam 3 Chapters 7 & 9

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

CSC 373 Sections 501, 510 Winter 2015 C Examples

Midterm Practice Exam

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Midterm Exam. Sample Solutions

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Computer Science & Engineering 150A Problem Solving Using Computers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CS16 Exam #1 7/17/ Minutes 100 Points total

Exercise 1.1 Hello world

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall What is your name?: (3 points for writing it on your answer sheet)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

I. True/False: (2 points each)

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

CS 115 Exam 3, Spring 2010

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

CSC 270 Survey of Programming Languages. What is a Pointer?

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Additional Questions

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Program Organization and Comments

Structures in C. C allows you to declare a struct. Once you do so, you can create variables of this type. Here is the general syntax:

WARM UP LESSONS BARE BASICS

'C' Programming Language

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Getting started with C++ (Part 2)

Integer Data Types. Data Type. Data Types. int, short int, long int

CS2141 Software Development using C/C++ C++ Basics

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

C++ Structures Programming Workshop 2 (CSCI 1061U)

Pointers, Dynamic Data, and Reference Types

Do not turn to the next page until the start of the exam.

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Programming in C++: Assignment Week 1

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Subject: Fundamental of Computer Programming 2068

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

A First Program - Greeting.cpp

Chapter 2 - Introduction to C Programming

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS2255 HOMEWORK #1 Fall 2012

CS349/SE382 A1 C Programming Tutorial

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

CSCE Practice Midterm. Data Types

Comp Sci 1MD3 Mid-Term II 2003 answers

Transcription:

CS 141 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015 You may take this test with you after the test, but you must turn in your answer sheet. This test has the following sections: I. True/False... 40 points; (20 questions, 2 points each) II. Multiple Choice... 60 points; (12 questions, 5 points each) --------------- 100 points total This test is worth 15% of your final grade. You must put your answers on the bubble form. This test is open book and open notes. For the multiple choice problems, select the best answer for each one and select the appropriate letter on your answer sheet. Be careful - more than one answer may seem to be correct. Some questions are tricky. When a problem describes a segment or fragment of code you may assume the reset of the program is correct and would be supplied to make it work. I. True/False: (2 points each) On your bubble form fill out A for true and B for false. T F 1. T F 2. In the code shown below, the comparison y==5 will never be done: int x=2, y=5; if ( (x >= 3) && (y == 5)) printf("yes "); Two different functions in C can have the same name, as long as the types or number of parameters are different. T F 3. The code below will print out: A int x=65; printf("%c", x); T F 4. The code below will print out: 3 char c='3'; printf("%d", c-'0'); T F 5. In a scanf statement the format specifier describes the format of the input. We can use %d for double-precision floating point numbers. T F 6. T F 7. T F 8. T F 9. T F 10. Most C programs can run in a C++ compiler. Most C++ programs can run in a C compiler. At most 10 guesses are needed when using Binary Search on a list of 1,000 elements. The name of an array is a constant address of the first element. & is the dereference operator in C CS 141, Midterm #2, Fall 2015 page 1

T F 11. T F 12. * is the address operator in C The code below will compile and run: int numbers[]=1,3,4; for( int i=0; i<5; i++) printf( %d, numbers[ i]); T F 13. T F 14. If two strings are not equal in C, then the strcmp function always returns a non-zero positive number. When we use ++ to increment a pointer variable in C, under some conditions we add 1 to it, but other times we add 2 or 4. For the next three problems consider the following declaration: struct Person char name[ 25]; int age; ; Person p1 = "Erin", 23; Person *pperson = &p1; T F 15. Would the following code compile and run and allow storing first name and age properly? printf("enter first name and age: "); scanf("%s %d", p1.name, *pperson.age); T F 16. Would the following code compile and run and allow storing first name and age properly? printf("enter first name and age: "); scanf("%s %d", p1.name, &(*pperson).age); T F 17. Would the following code compile and run and allow storing first name and age properly? printf("enter first name and age: "); scanf("%s %d", p1.name, &pperson->age); CS 141, Midterm #2, Fall 2015 page 2

Assume the following function is declared for the following 3 problems: int * add( int newnumber, int *parray, int &size) int *pnewarray = new int[ size + 1]; for( int i=0; i<size; i++) pnewarray[ i] = parray[ i]; pnewarray[ size] = newnumber; delete( parray); size++; return pnewarray; T F 18. T F 19. T F 20. Function add( ) shown above could be called using the following code and would run correctly: int numbers[] = 1,3,5; add( 7, numbers, size); Function add( ) shown above could be called using the following code: int *pnumbers = new int( size); pnumbers[0]=1; pnumbers[1]=3; pnumbers[2]=5; pnumbers = add( 7, pnumbers, size); Function add( ) shown above is equivalent to function add2( ) shown below void add2( int newnumber, int * *parray, int *size) int *pnewarray = new int[ *size + 1]; for( int i=0; i<*size; i++) pnewarray[ i] = (*parray)[ i]; pnewarray[ *size] = newnumber; delete( *parray); *parray = pnewarray; (*size)++; when called as follows: int *pnumbers = new int( size); pnumbers[0]=1; pnumbers[1]=3; pnumbers[2]=5; add2( 7, &pnumbers, &size); CS 141, Midterm #2, Fall 2015 page 3

II. Multiple Choice (6 points each) 21. What is the output from the following code when calling function pointerincrementproblem()? void foo( int *parray) *(parray++); (*parray)++; void pointerincrementproblem() int thearray[ ] = 1,3,5,7,8,9,10; foo( thearray); cout << "Pointer increment " << thearray[0] << " " << thearray[1] << " " << thearray[ 2] << endl ; a) Pointer increment 1 3 5 b) Pointer increment 2 3 5 c) Pointer increment 1 4 5 d) Pointer increment 1 3 6 22. What is the output from the following C program? #include <stdio.h> int x=2; void f1( int x) x++; void f2( int y) x++; int main() int x=2; f1( x); f2( x); printf("x is: %d \n", x); return 0; a) x is: 1 b) x is: 2 c) x is: 3 d) x is: 4 CS 141, Midterm #2, Fall 2015 page 4

23. What is the result of calling function testchangetwo() below, as part of a C++ program? void changetwo( int a, int &b) int temp = b++; b = a++; a = temp; void testchangetwo() int x=3; int y=5; changetwo( x, y); printf("x and y are: %d %d\n", x, y); a) x and y are: 3 5 b) x and y are: 5 3 c) x and y are: 3 3 d) x and y are: 5 5 24. When a 2-D array is passed to a function, in the function declaration the size of the first parameter may be left blank, but the second dimension must be supplied. Why is this? a) The total size of the array divided by the size given in the second dimension is used internally to calculate the number for the first dimension, so the user need not supply it. b) The size of the first dimension is always automatically supplied, even when not specified by the user, since it is included as part of the definition of every array. c) Code in C can overwrite the end of an array, however for a 2-D array the formula used to find the i th row needs to know how many columns are on each row. d) A NULL character is always inserted at the end of every row so that the compiler can tell where one row ends and the next begins, so the size of the first dimension is not necessary. 25. Consider the function shown at right below. For positive numbers, how would you best describe the return value of calling function first? a) x + n b) x * x c) x * n d) x int first( int x, int n) if( n==0) return 0; else if( n>0) return first( x, n-1) + x; return 0; 26. Consider the function shown at right below. When f5() is called and input is abc! then what is the output? a) abc! b) abc c)!cba d) cba void f5() char c; cin >> c; if (c!= '!') f5(); cout << c; CS 141, Midterm #2, Fall 2015 page 5

27. Consider the code segment shown below. If after the function call the value of number has changed, what is the most likely cause? int number = 5; printf(" %d", number); char letters[]="abcd"; changeletters( letters); printf(" %d", number); a) number is a global variable instead of a local variable as it should be b) Although number is not passed to function changeletters(), function changeletters() itself calls a second function which changes number c) Function changeletters() overwrites the end of array letters d) There is some ASCII control characters that are present in the code even though they are not visible 28. What is the output of the C++ program shown at right below? a) x + y = 2 b) x + y = 4 c) x + y = 8 d) x + y = 10 #include <iostream> using namespace std; int x=3, y=1; void confuse1(int &y, int x) x++; y++; void confuse2(int b, int *x) y = ++(*x); *x = b; void confuse3(int &a, int *x) a = *x + 1; (*x)++; int main() int x=2; confuse1( x, y); confuse2( x, &y); confuse3( x, &y); printf("x + y = %d \n", x+y); return 0; CS 141, Midterm #2, Fall 2015 page 6

29. What is the result of calling function arraypointertobraces() in the code below? void displayusingbraces( int thearray[], int size) printf("using braces Array is: "); for( int i=0; i<size; i++) printf("%d ", thearray[ i]); printf("\n"); void arraypointertobraces() int *parray = NULL; parray = (int *) malloc( sizeof( int) * size); // store some initial values into array int *ptemp = parray; *ptemp = 9; ptemp++; *ptemp = 11; ptemp++; *ptemp = 4; ptemp++; displayusingbraces( parray, size); a) Contents of the dynamically created array are printed out b) Compiler error in function arraypointertobraces() c) Compiler error in function displayusingbraces() d) Run-time error 30. What is the result of calling function arraybracestopointer() in the code below? void displayusingpointer( int *parray, int size) printf("using pointers Array is: "); for( int i=0; i<size; i++) printf("%d ", *parray); parray++; printf("\n"); void arraybracestopointer() const int thearray[ size]; // store some initial values into array thearray[ 0] = 9; thearray[ 1] = 11; thearray[ 2] = 4; displayusingpointer( thearray, size); a) Contents of the array are printed out b) Compiler error in function arraybracestopointer() c) Compiler error in function displayusingpointer() d) Run-time error CS 141, Midterm #2, Fall 2015 page 7

31. What is the result of calling function arraypointerchange() in the code below? void displayusingpointer( int *parray, int size) printf("using pointers Array is: "); for( int i=0; i<size; i++) printf("%d ", *parray); parray++; printf("\n"); void arraypointerchange() int *parray = NULL; parray = (int *) malloc( sizeof( int) * size); // store some initial values into array int *ptemp = parray; *ptemp = 9; ptemp++; *ptemp = 11; ptemp++; *ptemp = 4; ptemp++; displayusingpointer( parray, size); printf( "parray now points to %d\n", *parray); a) Contents of the array are printed out and parray points to the beginning of the array b) Contents of the array are printed out and parray points past the end of the array c) Compiler error in function arraypointerchange() d) Compiler error in function displayusingpointer() e) Run-time error 32. Consider the code shown below. Assuming that parameter size contains the size of the array, what ends up in array letters? void change( char letters[], int size) for( int i=0; i<size/2; i++) char temp = letters[i]; letters[ i] = letters[ size - i - 1]; letters[ size - i - 1] = temp; a) the letters from array letters in reverse order b) the letters from array letters in their original order c) the letters from array letters in reverse order when the length of letters is odd d) the letters from array letters in reverse order when the length of letters is even CS 141, Midterm #2, Fall 2015 page 8