Programming in C++: Assignment Week 1

Similar documents
Programming in C++: Assignment Week 3

Programming in C++: Assignment Week 3

Bruce Merry. IOI Training Dec 2013

Programming in C++: Assignment Week 2

Programming in C++: Assignment Week 8

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Unit 14. Passing Arrays & C++ Strings

CMPS 221 Sample Final

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

Functions. Arash Rafiey. September 26, 2017

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

Object Oriented Pragramming (22316)

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

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

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

Fundamentals of Programming Session 20

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

CS349/SE382 A1 C Programming Tutorial

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

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

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

First of all, it is a variable, just like other variables you studied

Introduction to C++ Systems Programming

Variation of Pointers

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

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

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

Introduction to C Programming Ray Lauff Temple University Computer Science v2.0

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

Programming in C++: Assignment Week 6

Principles of C and Memory Management

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

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

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)

Apllications. February 23, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications. pointers and const

Programming. C++ Basics

CMSC 202 Midterm Exam 1 Fall 2015

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

Principles of Programming. Chapter 6: Arrays

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Module Operator Overloading and Type Conversion. Table of Contents

Basic and Practice in Programming Lab7

High Performance Programming Programming in C part 1

CS242 COMPUTER PROGRAMMING

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

COMSW Introduction to Computer Programming in C

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Q 1. Attempt any TEN of the following:

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.


Introduction to C++ Introduction and History. Characteristics of C++

Increment and the While. Class 15

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

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

Programming in C++: Programming Test-1

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

Lab Instructor : Jean Lai

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Chapter 8 Character Arrays and Strings

Introduction to C++ Introduction to C++ 1

Pointers, Dynamic Data, and Reference Types

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

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

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Fall 2018 Discussion 2: September 3, 2018

W3101: Programming Languages C++ Ramana Isukapalli

A First Program - Greeting.cpp

CS13002 Programming and Data Structures, Spring 2005

! 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)

AN OVERVIEW OF C++ 1

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Programming in C++: Programming Test-2

Programming (1.0hour)

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

Computer Organization & Systems Exam I Example Questions

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

CSI33 Data Structures

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

EE 312 Fall 2017 Midterm 1 October 12, 2017

6.096 Introduction to C++ January (IAP) 2009

Computers Programming Course 5. Iulian Năstac

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

Lecture 02 C FUNDAMENTALS

Procedural Programming & Fundamentals of Programming

Presented By : Gaurav Juneja

C++ basics Getting started with, and Data Types.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

C++ For Science and Engineering Lecture 15

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Transcription:

Programming in C++: Assignment Week 1 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com February 24, 2017 Question 1 Which special symbol allowed in a variable name? Mark 1 a)! b) c) * d) _ Answer: d) Question 2 As per the Syntax of the language. Refer Slides Which of the following are unary operators in C? Mark 1 a)?: b) ++ c) *= d) sizeof() Answer: b) d) Question 3 As per the Syntax of the language. Refer Slides Which of the following declarations are correct? Mark 1 a) struct mystruct {int a;; b) struct {int a; c) struct mystruct {int a; d) struct mystruct int a; Answer: a) As per the Syntax of the language. Refer Slides. 1

Question 4 What will the function Sum return? Mark 1 void sum(int x, int y) { x++; y++; return (y); a) The incremented value of y b) The incremented value of y; the value of x is incremented but not returned c) Compilation Error: return value type does not match the function type d) Does not incremented value of y Answer: c) returned. The return type of the function is void, hence an integer value cannot be Question 5 What value will be printed for data.c? Marks 2 #include<stdio.h> #include <string.h> union Data { int i; unsigned char c; data; data.c = C ; data.i = 89; printf( "%c\n", data.c); a) C b) Y: ASCII 89 c) G d) C89 Answer: b) ASCII When %c is used for printing an integer value, conversion to the equivalent 2

Question 6 What is the output of the above program? Marks 2 #include <stdio.h> void foo( int[] ); int myarray[4] = {1, 2, 3, 0; foo(myarray); printf("%d ", myarray[0]); void foo(int p[4]){ int k = 34; p = &k; printf("%d ", p[0]); a) 1 2 b) 1 3 c) Will always output 1 d) 34 1 Answer: d) The base pointer of the array is used to point to an integer 34. In main, the array is accesssed directly to print the 1st element. Question 7 What is the output of the following program? Marks 2 #include <stdio.h> #define func(x, y) x / y + x int i = -6, j = 3; printf("%d\n",func(i + j, 3)); a) divide by zero error b) -4 c) -8 d) 3 Answer: c) x/y+x replaced by i + j/3 + i + j i.e (-6 + 3/3-6 +3) = (-6 + 1-6 +3) = -8 3

Question 8 What will be the output of the following program? Marks 2 #include <stdio.h> int sum(int a, int b, int c) { return a + b + c / 2; void main() { int (*function_pointer)(int, int, int); function_pointer = sum; printf("%d", function_pointer(2, 3, 4)); a) Compilation Error: Error in function call b) 7 c) 4.5 d) 5.5 Answer: b) function pointer is a pointer defined for any function with 3 integer parameters and integer return type. It points to function sum and returns the result of the sum. Question 9 Fill in the blank to concatenate strings str1 and str2 to form str3? Marks 2 #include <iostream> #include <string> int main(void) { string str1 = "I "; string str2 = "Travel"; string str3 = ; cout << str3; Output: I Travel a) str1+str2 b) strcat(str1,str2); c) strcat(strcpy(str3,str1),str2); d) str1.append(str2) Answer: a) str1+str2 and d) str1.append(str2) str1 and str 2 are two string type variables, operations possible for concatenation are str1+str2 (String is a stl, hence has + operator overloaded) and str1.append(str2) to append strings. 4

Question 10 What will be the output of the following program? Marks 2 #include <iostream> #include <algorithm> bool srt (int i, int j) { return (i < j); int data[] = {52, 76, 19, 5, 10, 100, 56, 98, 17; sort (data + 1, data + 4, srt); for (int i = 0; i < 9; i++) cout << data[i] << " "; a) 52 5 19 76 10 100 56 98 17 b) 52 76 19 10 5 100 56 98 17 c) 76 5 10 19 76 100 56 98 17 d) 76 5 10 19 76 Answer: a) The whole array is not passed for sorting, only from index 1 (data + 1, i.e 0 + 1) to index 4 (data + 4, i.e 0 + 4), i. e 3 elements, 76, 19, 5 Question 11 What will be the output of the following program? Marks 2 #include<iostream> #include<string.h> #include<stack> char str[19]= "Programming"; stack<char> s; for(int i = 0; i < strlen(str); i++) s.push(str[i]); for(int i = 0; i < strlen(str) - 1; i++) { cout << s.top(); s.pop(); a) rogramming b) ogramming c) gnimmargor 5

d) gnigormmar Answer: c) When programming pushed to stack, the element on the top is g (gnimmargorp), which is displayed and then popped. Continues till length of str - 1, hence p not printed at the end. Question 12 Fill up the blanks for A# and B# below: Marks 2 #include <iostream> #include <vector> cout << "Enter the no. of elements: "; int count, j, sum=0; cin >> count; A# // Declare with Default size B# // Change the size to the required amount for(int i = 0; i < arr.size(); i++) { arr[i] = i; sum + = arr[i]; cout << "Array Sum: " << sum << endl; a) A#: vector <int> arr(count); B#: arr.resize(count); b) A#: vector <int> arr(count); B#: arr.size(count); c) A#: vector <int> arr; B#: arr.size(count); d) A#: vector <int> arr; B#: arr.resize(count); Answer: d) As per syntax, using resize operator 6