C++FA 5.1 PRACTICE MID-TERM EXAM



Similar documents
C++FA 3.1 OPTIMIZING C++

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

C++ INTERVIEW QUESTIONS

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

The C Programming Language course syllabus associate level

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Ch 7-1. Object-Oriented Programming and Classes

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

C++ Overloading, Constructors, Assignment operator

13 Classes & Objects with Constructors/Destructors

Chapter 5 Functions. Introducing Functions

Note: Syntactically, a ; is needed at the end of a struct definition.

C++ Programming Language

Java Interview Questions and Answers

Statements and Control Flow

Sources: On the Web: Slides will be available on:

AP Computer Science Java Subset

Object Oriented Software Design

An Incomplete C++ Primer. University of Wyoming MA 5310

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Class 16: Function Parameters and Polymorphism

arrays C Programming Language - Arrays

Basics of C++ and object orientation in OpenFOAM

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

Object Oriented Software Design II

Binary storage of graphs and related data

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Classes and Pointers: Some Peculiarities (cont d.)

Semantic Analysis: Types and Type Checking

Course notes Standard C++ programming

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

Course Title: Software Development

Object Oriented Software Design II

Object Oriented Software Design II

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

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

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

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Tutorial on C Language Programming

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

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

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

CORBA Programming with TAOX11. The C++11 CORBA Implementation

N3458: Simple Database Integration in C++11

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

El Dorado Union High School District Educational Services

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

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Introduction to Java

Member Functions of the istream Class

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Java (12 Weeks) Introduction to Java Programming Language

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Compiler Construction

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

OpenCL Static C++ Kernel Language Extension

Curriculum Map. Discipline: Computer Science Course: C++

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Passing 1D arrays to functions.

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Common Beginner C++ Programming Mistakes

Java Application Developer Certificate Program Competencies

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Answers to Review Questions Chapter 7

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Lecture 2 Notes: Flow of Control

C++ Crash Kurs. C++ Object-Oriented Programming

Introduction to Programming Block Tutorial C/C++

Advanced Data Structures

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Stacks. Linear data structures

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Konzepte objektorientierter Programmierung

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Comp151. Definitions & Declarations

Example of a Java program

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

Help on the Embedded Software Block

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Chapter 5. Selection 5-1

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

CIS 190: C/C++ Programming. Polymorphism

Conditions & Boolean Expressions

Transcription:

C++FA 5.1 PRACTICE MID-TERM EXAM This practicemid-term exam covers sections C++FA 1.1 through C++FA 1.4 of C++ with Financial Applications by Ben Van Vliet, available at www.benvanvliet.net. 1.) A pointer is: - An address variable. A variable containing the location of an existing mailbox. Another name on the address of an existing memory location. The address of a value type or reference type. 2.) In the following code, x could be a: - Number x; 1. An enum. 2. A structure. 3. A typedef. 4. An object. 2., or 4. 1., 3., or 4. 1., 2., 3., or 4. 2., 3., or 4. 3.) Which of the following would necessarily imply function overriding? - Two functions where the definition of one replaces the other. Two functions with the different signatures. Two virtual functions that are defined in the base class. Two class member functions with the same name.

For questions 4-8, consider the following lines of code in the boxes in sequence: int a[ 7 ] = 5, 6, 3, 8, 7, 1, 2 ; int *b; b = a; 4.) What is the value of ( a + 2 )? - The address of 6. The address of 3. 6. 3. 5.) What is the value of b[ 3 ]? - The address of 8. The address of 7. 7. 8. If next the following line of code runs: b += 4; 6.) Now, what is the value of &( ( b 2 )[ 4 ] )? - 2. The address of 2. 1 The address of 1 If now the following line of code runs: ( *( ++b ) )++;

7.) Now, what is the value of b? - The address of 7. The address of 8. The address of 1. The address of 2. 8.) Now, what is the value of ( b - 2 )[ 3 ]? - 1. The address of 1. 2. The address of 2. 9.) The following line of code can best be described as: - void bar( int, void (*)[]( int )); The definition of a function that accepts an int and function pointer. A declaration with type parameters int and array of function pointers. The prototype of a function that points to another function. A function of type void that accepts an array of void pointers to ints.

For Questions 10-15, assume the following class definitions: class Instrument protected: int value; virtual void calc_value() value = 1; public: Instrument( int v ) : value( v ) cout << "Constructor running on " << value << endl; Instrument( Instrument &i ) value = i.get_value() + 1; ~Instrument() cout << "Destructor running on " << value << endl; Instrument operator+( Instrument &i ) return Instrument( value + i.get_value() ); int get_value() calc_value(); return value; static void print_type() cout << "Instrument class!" << endl; ; class Bond : public Instrument protected: void calc_value() value = 3; public: Bond( int v ) : value( v ) ;

10.) The function Instrument( Instrument &i ) is: - A constructor overload. A constructor override. The copy constructor. The destructor. 11.) In the Instrument class, the calc_value function is: - Overridable. Pure virtual. Must override. Overloaded. 12.) The operator+ function requires a parameter because: - + is a binary operator. + is a function that requires a left-hand value. + is a unary operator. + implicitly creates a copy of the parameter. 13.) The print_type method: - Could not access the value data member. Can only be called from the class. Is not inherited because it is static. Would print Bond for the bond class. 14.) The Bond class: - Overloads the calc_value method. Must override the calc_value method. Will not inherit the base class operator+ method. Fails to call the base class constructor. 15.) The Instrument class destructor: - Runs only when an instance of Instrument is destroyed. Should be declared as virtual. Will not run when calling delete on a Bond. Must be overridden in a derived class.

16.) The following line of code is best described by: - MyClass::MyClass( int v ) : value( v ) A.cpp file class method. A constructor prototype. A static method call. A.h file copy constructor. 17.) Given the following function: - void foo( int *x ) *x *= *x; *x = ( *x + ( 2 * *x ) ) + ( *x + 2 ); What is the output from the following code? int y = 5; foo( &y ); cout << y; 42 22 32 17 18.) What is the output from the following code? - int a[] = 8, 9, 10, 11, 12 ; ( *( a + 3 ) )++; cout << *a; 12 10 11. The address of 11.

19.) What is the output from the following code? - for( int x = 1; x < 10; x += 2 ) if ( x > 7 x <= 2 ) cout << x << ; x--; 1 3 7 9 1 3 9 1 2 7 9 1 2 9 20.) What is the output from the following code assuming &a = 100 and &b = 200 and &c = 300? - int a = 3; int *b = &a; int **c = &b; cout << c << " "; cout << *c << " "; cout << &( *c ) << " "; cout << &( **c ) << endl; 100 200 300 100 200 100 200 100 200 100 300 3 100 200 300 3

21.) What is the output from the following code? - int a[] = 7, 8, 3, 4 ; int *b = a; cout << a[ 1 ] << " "; cout << ( a + 2 )[ 1 ] << " "; cout << b[ 2 ] << " "; cout << *( ++b ) << " "; cout << ( b - 1 )[ 0 ] << " "; 8 4 3 8 8 8 4 4 8 8 8 4 4 8 4 8 4 3 8 7 22.) What is the purpose of the following line of code? - typedef double Real; Declare a variable called Real of type double. Define double as Real. Create a type called Real that is a double. Create Real as an instance of double. 23.) Given the code in question 22, what is the purpose of the following code? - Real values[ 5 ]; Create a values object. Create a two dimensional array of doubles. Create an array of Reals. Create a double typedef. 24.) If a and b are booleans, which answer is logically equivalent to the following statement? -!( a b ) == ( a && b )!a b a && b a b!( a == b )

25.) Given the following function: - int f( int &x ) x -= 3; cout << x << " "; x += x; return x; What is the output from the following code? int a = 2; int b = f( a ); cout << f( b ) << " "; cout << a << endl; -1-8 -10-2 1-6 -8 0-1 -8-10 -10-1 -6-2 -2 26.) What is the output from the following code? - cout << "a\t\tb\n\t\tc\n"; a b c a b c a b c a b c

For Questions 27-31, assume the following class definition: class MyClass private: int v; public: MyClass( int a = 2 ) : v( a ) cout << v << is alive. << endl; ~MyClass(void) cout << v << is dying. << endl; ; MyClass operator-( MyClass &c ) MyClass r( v - c.get_value() * 3 ); return r; void set_value( int a ) v = a; int get_value() return v;

27.) What is the output from the following code? - if ( true ) MyClass m_obj( 4 ); cout << m_obj.get_value() << " "; 4 is alive. 4 4 is dying. 4 4 is dying. 4 is alive. 4 4 28.) What is the output from the following code? - if ( true ) MyClass *m_obj = new MyClass; 2 is alive. [Garbage value] is alive. is alive. is dying. Compile error. 29.) Not including the constructor and destructor code, what is the output from the following code? MyClass m_obj1( 4 ); MyClass m_obj2( 5 ); MyClass m_obj3 = m_obj1 - m_obj2; cout << m_obj3.get_value() << endl; -9-11 Compile error. Runtime error.

30.) Not including the constructor and destructor code, what is the output from the following code? - MyClass *m_obj1 = new MyClass( 4 ); MyClass *m_obj2 = new MyClass( 6 ); MyClass m_obj3 = *m_obj1 - *m_obj2; cout << m_obj3.get_value() << endl; -2 Compile error. Runtime error. -14 31.) What is the output from the following code? - int main() MyClass m_obj1(4); MyClass *m_obj2 = new MyClass(4); return 0; Nothing 4 is alive. 5 is alive. 4 is dying. 4 is alive. 4 is dying. 32.) What is logic error in the following code? - int foo( int a ) return a + foo( a - 1 ); Should be pass by reference. No termination condition. Return type is incorrect. Iteration is unnecessary.

33.) What is the syntax error in the following code? - int main() int size = 5; int a[ size ]; a[ 4 ] = 10; cout << *a << endl; return 0; Can t dereference constant pointer. Must declare array size with a literal integer value. Size must be constant. Index is out of bounds. 34.) What is the syntax error in the following code? - int x = 0; do cout << x << endl; while ( x < 0 ) This code will never run. While loop syntax is incorrect. X does not have scope in the loop. Missing ;. 35.) Given a class called MyClass, what is the syntax error in the following code? - MyClass operator@( MyClass &x ) return MyClass( x.get_value() ); Can t return nameless object. Returning by value causes the copy constructor to run. Cannot overload this operator. No call to left-hand value in definition.

36.) What is a friend? - A class that has access to data in a parent class. A class that has access to data in another class. A class that has access to data in a child class A class that has access to data in a static class. 37.) What is the output from the following code? - int a = 3, b = 3, c = 1; a += b - c; b += a * c; c += a - b; cout << a << " " << b << " " << c << endl; 3 4 0 5 8-2 5 10 0 3 5-5 38.) What is the output from the following code? - int x = 17, y = 7; while ( x > 0 ) y += 3; x -= 5; cout << x << " " << y << endl; -1 19-1 31-3 19-2 19

39.) Given the following two functions: - int f( int x ) return x + 2; int g( int x ) return x * 2; What is the output from the following code? int x = 1; x += f( g( x ) ) + g( f( x ) ); cout << x; 11 3-1 9

40.) What is the output from the following code? - int main() int a = 1; int b = 1; while ( b < 3 ) switch ( a ) case 0: b--; case 1: a--; break; case 3: b++; case 4: a++; break; default: b++; cout << b << " "; return 0; 1 0 1 3 1 0 1 2 3 1 1 2 3 1 0 1 1 2 3 41.) Polymorphism means basically: - Overloaded definitions at the base class level. Derived classes inherit base class definitions. A group of methods with many implementations. Function overloading as well as overriding.

42.) After including cstdlib, what is the output from the following code? - int main() int a = 1234; char *s = "2309"; printf( "%s!= %i\n", s, a ); double d = atof( "3245" ); printf( "%s!= %e\n", s, d ); return 0; Compile error. true true 2309!= 1234 2309!= 3245 2309!= 1234 Runtime error. 43.) What is the output from the following code? - int foo( int [], int, int (*)( int, int ) ); int bar( int, int ); int main() int a[] = 6, 1, 7, 3, 8 ; cout << foo( a, 5, bar ) << endl; return 0; int foo( int a[], int c, int ( *f )( int, int ) ) int v = 0; for( int i = 0; i < c; i++ ) v += f( a[ i ], c ); return v; int bar( int a, int b ) return a - b; 8 0 50 1

44.) Ignoring prototypes, what is the output from the following code? - int main() int b = 2; cout << foo( b ) << endl; cout << b << endl; return 0; int day( int b ) b++; cout << bar( &b ) << endl; return b; int foo( int &b ) b++; day( b ); return b; int bar( int *b ) *b += *b; return *b; 8 3 2 3 3 3 3 3 2 8 3 3

45.) What is the output from the following code? - int main() int a = 37; for( int b = 128; b > 0; b /= 2 ) if ( a & b ) cout << "1 "; else cout << "0 "; cout << endl; return 0; 00100101 01000011 00011111 00101111 46.) What is the point of the following code? - #define Par 1000.00 enum instr_type Stock,Option,Future,Bond ; typedef double price; struct Tick price Price; instr_type Type; ; int main() Tick a; a.price = Par; a.type = Bond; return 0; To obfuscate the code (i.e. to make it more confusing). To enable object oriented design. To make it more human-readable. To speed up execution of the code.

47.) What is the output from the following code? - char name[] = "John Doe"; int n = 0; for( ; name[ n ]!= '\0' ; ) cout << name[ n ] << endl; n++; John Doe, horizontally. John Doe, vertically. Compile error. John, vertically. 48.) What is the point of the following code? - typedef float price; price prices[]; To create a two dimensional array of floats. To create an array of prices. To declare price as a variable of type float. To define price as a pointer to an array. 49.) What is the output from the following code? - #define IBM "IBM" int main() cout << IBM << endl; return 0; true Compile error. IBM 0

50.) What is the output from the following code? - enum Pos LONG,SHORT ; Pos p; p = ( 3 > 2? SHORT : LONG ); cout << p << endl; 0 1 LONG SHORT