Programming in C++: Assignment Week 2

Similar documents
Programming in C++: Assignment Week 1

Programming in C++: Assignment Week 3

Pointers, Dynamic Data, and Reference Types

CS201- Introduction to Programming Current Quizzes

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Programming in C++: Assignment Week 4

Programming in C++: Assignment Week 3

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Introduction to C++ Systems Programming

Contents. C++ As a Better C. Comments. User-defined Type Names

C++ As a Better C. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Chapter 15 - C++ As A "Better C"

C PROGRAMMING Lecture 4. 1st semester

Fast Introduction to Object Oriented Programming and C++

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

C Pointers. 6th April 2017 Giulio Picierro

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

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

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Pointers. Developed By Ms. K.M.Sanghavi

CMSC 202 Midterm Exam 1 Fall 2015

Why VC++ instead of Dev C++?

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Introduction to C++ 1

Exam 3 Chapters 7 & 9

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CS 115 Exam 3, Spring 2010

Programming in C++: Assignment Week 4

Memory and Pointers written by Cathy Saxton

Memory, Arrays, and Parameters

beginslide Chapter 6 The C++ Programming Language The Role of Classes

The University of Nottingham

Pointers (1A) Young Won Lim 11/1/17

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Homework #3 CS2255 Fall 2012

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Short Notes of CS201

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

The University of Nottingham

Midterm Review. PIC 10B Spring 2018

CS349/SE382 A1 C Programming Tutorial

CS201 - Introduction to Programming Glossary By

Name. CPTR246 Spring '17 (100 total points) Exam 2

CSE 333 Midterm Exam 5/10/13

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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

Applications of Pointers (1A) Young Won Lim 12/26/17

Object oriented programming C++

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

What is an algorithm?

CSCE Practice Midterm. Data Types

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Fundamental of Programming (C)

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Lab Instructor : Jean Lai

Tema 6: Dynamic memory

Programming in C++: Programming Test-2

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

ComS 228 Exam 1. September 27, 2004

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Dynamic Allocation of Memory

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

CSCE 110 PROGRAMMING FUNDAMENTALS

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CS

pointers + memory double x; string a; int x; main overhead int y; main overhead

Linked List using a Sentinel

MTH 307/417/515 Final Exam Solutions

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Tokens, Expressions and Control Structures

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS 103 Unit 11. Linked Lists. Mark Redekopp

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

CS201 Some Important Definitions

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

EECS402 Lecture 02. Functions. Function Prototype

Homework Assignment #2 (revised)

Lecture 2: C Programm

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

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

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Technical Questions. Q 1) What are the key features in C programming language?

Solutions to Assessment

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

COMP322 - Introduction to C++

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Transcription:

Programming in C++: Assignment Week 2 Total Marks : 20 Each question carries one mark Right hand side of each question shows its Type (MCQ/MSQ) March 3, 2017 Question 1 Look at the code snippet below: int * const p = &n; Which of the following statement is true for the variable p? Mark 1 a. const-pointer to non-const-pointee b. non-const-pointer to const-pointee c. const-pointer to const-pointee d. non-const-pointer to non-const-pointee Answer: a Explanation: As per syntax, refer slides Question 2 Look at the following code segment and decide which statement(s) is/are correct. Mark 1 int main(){ int m = 4; const int n = 5; const int * p = &n; int * const q = &m; //... n = 6; // stmt-1 *p = 7; // stmt-2 p = &m; // stmt-3 *q = 8; // stmt-4 q = &n; // stmt-5 //... a. stmt-1 b. stmt-2 c. stmt-3 d. stmt-4 1

e. stmt-5 Answer: c, d Explanation: As per syntax, refer slides Question 3 Identify the output of the following code. Mark 1 Mark 1 #include<iostream> typedef struct Complex { double re; double im; Complex; const Complex c = {2,4 ; c.re = 5.9; cout << c.re; a. 5.9 b. Cannot assign an integer value to a double variable c. 5.90 d. Cannot assign value 5.9 to read only c.re Answer: d Explanation: c is variable of the structure Complex, but it is defined as const, hence cannot be modified Question 4 Identify the correct statement(s). Mark 1 #include <cmath> #define TWO 2 #define PI 4.0*atan(1.0) int r = 10; double peri = TWO * PI * r; cout << "Perimeter = " << peri << endl; a. TWO and PI are variables b. Types of TWO and PI may be indeterminate c. Types of TWO and PI are determinate 2

d. TWO and PI look like variables Answer: b), d) Explanation: TWO and PI are manifest constants, hence types can be indeterminate and look like variables. 3

Question 5 What will be the output of the following code? Mark 1 double Ref_const(const double &param) { return (param * 3.14); double x = 8, y; y = Ref_const(x); cout << x << " "<< y; a. Cannot return constant parameter b. Cannot edit constant parameter c. 8 26 d. 8 25.12 Answer: d) Explanation: Const used to pass reference parameter param to prevent from being modified. The value of param is used only Question 6 What will be the output of the following code? Mark 1 void func(int n1 = 10, int n2) { cout <<n1 << " "<< n2; func(1); func(3, 4); a. 1 10 3 4 b. 10 1 4 3 c. 10 1 3 4 d. Compilation error: Argument missing for parameter 2 of func Answer: d) Explanation: Default values needs to specified from the end, hence function resoultion fails 4

Question 7 What will be the output of the following code? Mark 1 int Add(int a, int b) { return (a + b); double Add(double c) { return (c + 1); int x = 1, y = 2, z; z = Add(x, y); cout << z; double s = 4.5, u; u = Add(s); cout << " " << u << endl; a. Add cannot be overloaded with different return types b. Add cannot be resolved c. 3 5.5 d. 3 6.5 Answer: c) Explanation: Question 8 Two versions of function Add called as per resolution Which function prototype will match the function call func(3.6,7)? Mark 1 void func(int, int); // Proto 1 void func(double, double, double = 5.6); // Proto 2 void func(double, double, char = c ); // Proto 3 void func(double, char = d, char = c ); // Proto 4 a. Proto 1 b. Proto 2 c. Proto 3 d. Proto 4 Answer: a), b), c) Explanation: Proto 1 allowed, as 3.6(1st parameter) is downcast to integer. Proto 2 allowed, as default value will be used for third parameter. Proto 3 allowed, default value and type will be used for third parameter. Proto 4 fils for mismatch in 2nd parameter 5

Question 9 What will be the output of the following code? Mark 1 #include<iostream> int *ptr = NULL; cout << " Output: In Program"; delete ptr; a. ptr cannot point to NULL b. delete ptr (NULL) causes program crash c. Output: In Program d. Invalid Syntax Answer: c) Explanation: Null assignment to pointer allowed. Normal print provides the output Question 10 Fill up the blanks to get the desired output according to the test cases. Mark 1 #include <cstring> #include <cstdlib> typedef struct _String { char *str; String; { String s; s.str = (char *) malloc(strlen(s1.str) + strlen(s2.str) + 1); strcpy(s.str, s1.str); strcat(s.str, s2.str); return s; String s1, s2, s3; s1.str = strdup("i"); s2.str = strdup(" love Travelling "); s3 = s1 + s2; cout << s3.str << endl; a. String + operator(const String& s1, const String& s2) 6

b. String +(const String& s1, const String& s2) c. String operator+(const String& s1, const String& s2) d. string operator+(const String s1, const String& s2) Answer: c) Explanation: As per syntax, Overloading operator + for String structure. Reference parameters passed as const to prevent modification. I Programming Assignments Question 1 Fill up the blanks by providing appropriate return type and argument type for the function Ref func() to get the desired output according to the test cases. Marks 2 Ref_func( param) { return (++param); int x, y, z; cin >> x ; cin >> y ; y = Ref_func(x); cout << x << " "<< y << endl; Ref_func(x) = z; cout << x << " "<< y; Input: 8, -9 Output: 9 9-9 9 Input: 10, 20 Output: 11 11 20 11 Input: -19, -32 Output: -18-18 -32-18 Answer: //int& // int & 7

Question 2 Fill up the blanks to get the desired output according to the test cases. Marks 2 #include <stdio.h> int func(int, int); #define func(x, y) / + int i,j; // Complete the Macro definition scanf("%d", &i); scanf("%d", &j); printf("%d ",func(i + j, 3)); func // Fill the blank printf("%d\n",func(i + j, 3)); int func(int x, int y) { return x / y + x; a. Input: -6, 3 Output: -8-4 b. Input: 11, 15 Output: 42 34 c. Input: -4, -8 Output: -18-16 Answer: #define func(x, y) x / y + x #undef func Question 3 Fill up the blanks with appropriate keyword to get the desired output according to the test cases. Marks 2 int SQUARE(int x) { x * x; int a, b, c; cin >> a ; b = SQUARE(a); cout << "Square = " << b << ", "; c = SQUARE(++a); cout << "++ Square = " << c ; a. Input: 4 Output:Square = 16, ++ Square = 25 b. Input: -8 Output: Square = 64, ++ Square = 49 c. Input: -10.5 Output: Square = 100, ++ Square = 81 Answer: inline // return 8

Question 4 Fill up the blanks to get the desired output according to the test cases in the perspective of dynamic memory allocation and de-allocation. Marks 2 int main(){ int d; // use operator new to allocate memory to variable p cin>> d ; *p = d ; cout << ++*p + d++ * (++*p + *p); delete( ); // Fill the blank a. Input: -7 Output: 64 b. Input: 11.5 Output: 298 c. Input: 15 Output: 526 Answer: int *p = (int *)operator new(sizeof(int));// operator // p Question 5 Overload the function Area, by writing the appropriate definition in place of blank to get the desired output according to the test cases. Marks 2 include<iostream> // Overload the function Area // Write the definition of Area int x,y, t; double z, u, f; cin >> x >> y ; cin >> z >> u ; t = Area(x); cout << "Area = " << t << " "; f = Area(z); cout << "Area = " << f << " "; f = Area(z,u); cout << "Area = " << f ; a. Input: 8, 7, 9, 10 Output: Area =80 Area =90 Area =90 b. Input: 8, 9, 8.5, 9.5 Output: Area =80 Area =80 Area =80.75 9

c. Input: 7, 8, 7, 9.6 Output: Area =70 Area =70 Area =67.2 Answer: int Area(int a, int b = 10) { return (a * b); // double Area(double c, double d) { return (c * d); 10