C PROGRAMMING QUESTIONS AND

Similar documents
C Multiple Choice Questions and answers MCQ with Ans.

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

Sample Question Set For Coding and Debugging

BSM540 Basics of C Language

M1-R4: Programing and Problem Solving using C (JAN 2019)

2. Which of the following will print the value 2 for the above code?

COMP 2001/2401 Test #1 [out of 80 marks]

Week 1 Questions Question Options Answer & Explanation A. 10 B. 20 C. 21 D. 11. A. 97 B. 98 C. 99 D. a

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

OBJECTIVE QUESTIONS: Choose the correct alternative:

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Programming Multiple. Choice

(2) What is meaning of following pointer declaration? int(*(*ptr1)())[2];

Lecture 02 C FUNDAMENTALS

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Programming in C++ 4. The lexical basis of C++

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

Variation of Pointers

Computer Organization & Systems Exam I Example Questions

Introduction to C Language (M3-R )

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

High Performance Programming Programming in C part 1

Group of Institutions Test Paper: Technical (Set-4) T&P Department } (A) 0 (B) 25 (C) 1 (D) -1 (E) 2. } (A) Sachin (B) Rahul

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

MCAT113: Principles of Programming with C

Programming. Elementary Concepts

Practice Sheet #07 with Solutions

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

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

INTRODUCTION 1 AND REVIEW

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

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

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Review of the C Programming Language for Principles of Operating Systems

Lecture 03 Bits, Bytes and Data Types

THE FUNDAMENTAL DATA TYPES

VARIABLES AND CONSTANTS

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Basic Elements of C. Staff Incharge: S.Sasirekha

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

211: Computer Architecture Summer 2016

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

CS 0449 Sample Midterm

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

COMPUTER APPLICATION

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

ESC101N: Fundamentals of Computing End-sem st semester

Midterm Exam 2 Solutions, C programming

C Language, Token, Keywords, Constant, variable

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

DEPARTMENT OF MATHS, MJ COLLEGE

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

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

Introduction to C. Systems Programming Concepts

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Work relative to other classes

Advanced C Programming Topics

Review of the C Programming Language

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Fundamental of Programming (C)

M1-R4: Programing and Problem Solving using C (JULY 2018)

Q 1. Attempt any TEN of the following:

Problem Solving and 'C' Programming

F28HS2 Hardware-Software Interface. Lecture 3 - Programming in C 3

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

Fundamentals of Programming

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

BSM540 Basics of C Language

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

COMP 2355 Introduction to Systems Programming

Object Oriented Pragramming (22316)

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Library Functions. General Questions

More about BOOLEAN issues

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Name :. Roll No. :... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/ BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING

Model Viva Questions for Programming in C lab

Bit Manipulation in C

COMP1917 Computing 1 Written Exam Sample Questions

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

C Programming Class I

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

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

Compiling and Running a C Program in Unix

Transcription:

8/26/2011 C C PROGRAMMING QUESTIONS AND ANSWER http://cquestionbank.blogspot.com Ritesh kumar

(1) What will be output if you will compile and execute the following c code? struct marks{ int p:3; int c:3; int m:2; ; struct marks s={2,-6,5; printf("%d %d %d",s.p,s.c,s.m); (a) 2-6 5 (b) 2-6 1 (c) 2 2 1 (d) Compiler error (e) None of these Answer: (c) Binary value of 2: 00000010 (Select three two bit) Binary value of 6: 00000110 Binary value of -6: 11111001+1=11111010 (Select last three bit) Binary value of 5: 00000101 (Select last two bit) Complete memory representation: Page 2

(2) What will be output if you will compile and execute the following c code? int huge*p=(int huge*)0xc0563331; int huge*q=(int huge*)0xc2551341; *p=200; printf("%d",*q); (a)0 (b)garbage value (c)null (d) 200 (e)compiler error Answer: (d) Physical address of huge pointer p Huge address: 0XC0563331 Offset address: 0x3331 Segment address: 0XC056 Page 3

Physical address= Segment address * 0X10 + Offset address =0XC056 * 0X10 +0X3331 =0XC0560 + 0X3331 =0XC3891 Physical address of huge pointer q Huge address: 0XC2551341 Offset address: 0x1341 Segment address: 0XC255 Physical address= Segment address * 0X10 + Offset address =0XC255 * 0X10 +0X1341 =0XC2550 + 0X1341 =0XC3891 Since both huge pointers p and q are pointing same physical address so content of q will also same as content of q. (3) Write c program which display mouse pointer and position of pointer. (In x coordinate, y coordinate)? Answer: #include<dos.h> union REGS i,o; int x,y,k; //show mouse pointer i.x.ax=1; int86(0x33,&i,&o); while(!kbhit()) //its value will false when we hit key in the key board { Page 4

i.x.ax=3; //get mouse position x=o.x.cx; y=o.x.dx; printf("(%d, %d)",x,y); delay(250); int86(0x33,&i,&o); (4) Write a c program to create dos command: dir. Answer: Step 1: Write following code. #include <stdio.h> #include <dos.h> int main(int count,char *argv[]){ struct find_t q ; int a; if(count==1) argv[1]="*.*"; a = _dos_findfirst(argv[1],1,&q); if(a==0){ while (!a){ printf(" %s\n", q.name); a = _dos_findnext(&q); else{ printf("file not found"); Page 5

Step 2: Save the as list.c (You can give any name) Step 3: Compile and execute the file. Step 4: Write click on My computer of Window XP operating system and select properties. Step 5: Select Advanced -> Environment Variables Step 6: You will find following window: Click on new button (Button inside the red box) Step 7: Write following: Variable name: path Variable value: c:\tc\bin\list.c (Path where you have saved) Page 6

Step 8: Open command prompt and write list and press enter. (5) What will be output if you will compile and execute the following c code? int i; float a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=3;i++) printf("%d ",*ptr++); Page 7

(a)0 0 0 0 (b)garbage Garbage Garbage Garbage (c)102 56-80 32 (d)102 102-90 64 (e)compiler error Answer: (d) In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of float a=5.2 ptr pointer will point first fourth byte then third byte then second byte then first byte. Content of fourth byte: Binary value=01100110 Decimal value= 64+32+4+2=102 Content of third byte: Binary value=01100110 Decimal value=64+32+4+2=102 Content of second byte: Page 8

Binary value=10100110 Decimal value=-128+32+4+2=-90 Content of first byte: Binary value=01000000 Decimal value=64 Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit. (6) What will be output if you will compile and execute the following c code? int i; double a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=7;i++) printf("%d ",*ptr++); (a) -51-52 -52-52 -52-52 20 64 (b) 51 52 52 52 52 52 20 64 (c) Eight garbage values. (d) Compiler error (e) None of these Answer: (a) Page 9

In c double data type is eight byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of double a=5.2 ptr pointer will point first eighth byte then seventh byte then sixth byte then fifth byte then fourth byte then third byte then second byte then first byte as shown in above figure. Content of eighth byte: Binary value=11001101 Decimal value= -128+64+8+4+1=-51 Content of seventh byte: Page 10

Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of sixth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fifth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fourth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of third byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of second byte: Binary value=000010100 Decimal value=16+4=20 Content of first byte: Binary value=01000000 Decimal value=64 Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit. (7) What will be output if you will compile and execute the following c code? Page 11

printf("%s","c" "question" "bank"); (a) c question bank (b) c (c) bank (d) cquestionbank (e) Compiler error Answer: (d) In c string constant xy is same as x y (8) What will be output if you will compile and execute the following c code? char *str="c-pointer"; printf("%*.*s",10,7,str); (a) c-pointer (b) c-pointer (c) c-point (d) cpointer null null (e) c-point Answer: (e) Page 12

Meaning of %*.*s in the printf function: First * indicates the width i.e. how many spaces will take to print the string and second * indicates how many characters will print of any string. Following figure illustrates output of above code: (9) What will be output if you will compile and int a=-12; a=a>>3; printf("%d",a); (a) -4 (b) -3 (c) -2 (d) -96 (e) Compiler error Answer :( c) Binary value of 12 is: 00000000 00001100 Binary value of -12 wills 2 s complement of 12 i.e. Page 13

So binary value of -12 is: 11111111 11110100 Right shifting rule: Rule 1: If number is positive the fill vacant spaces in the left side by 0. Rule 2: If number is negative the fill vacant spaces in the left side by 1. In this case number is negative. So right shift all the binary digits by three space and fill vacant space by 1 as shown following figure: Since it is negative number so output will also a negative number but its 2 s complement. Page 14

Hence final output will be: And its decimal value is: 2 Hence output will be:-2 (10) What will be output if you will compile and #include <string.h> printf("%d %d",sizeof("string"),strlen("string")); (a) 6 6 (b) 7 7 (c) 6 7 (d) 7 6 (e) None of these Answer: (d) Page 15

Sizeof operator returns the size of string including null character while strlen function returns length of a string excluding null character. (11) What will be output if you will compile and static main; int x; x=call(main); printf("%d ",x); int call(int address){ address++; return address; (a) 0 (b) 1 (c) Garbage value (d) Compiler error (e) None of these Answer: (b) As we know main is not keyword of c but is special type of function. Word main can be name variable in the main and other functions. Page 16

(12) What will be output if you will compile and int a,b; a=1,3,15; b=(2,4,6); printf("%d ",a+b); (a) 3 (b) 21 (c) 17 (d) 7 (e) Compiler error Answer: (d) In c comma behaves as separator as well as operator. a=1, 3, 15; b= (2, 4, 6); In the above two statements comma is working as operator. Comma enjoys least precedence and associative is left to right. Assigning the priority of each operator in the first statement: Page 17

Hence 1 will assign to a. Assigning the priority of each operator in the second statement: (13) What will be output if you will compile and int extern x; int main() printf("%d",x); x=2; int x=23; (a) 0 (b) 2 (c) 23 (d) Compiler error (e) None of these Answer: (c) extern variables can search the declaration of variable anywhere in the program. Page 18

(14) What will be output if you will compile and int i=0; if(i==0){ i=((5,(i=3)),i=1); printf("%d",i); else printf("equal"); (a) 5 (b) 3 (c) 1 (d) equal (e) None of above Answer: (c) (15) What will be output if you will compile and int a=25; printf("%o %x",a,a); (a) 25 25 (b) 025 0x25 Page 19

(c) 12 42 (d) 31 19 (e) None of these Answer: (d) %o is used to print the number in octal number format. %x is used to print the number in hexadecimal number format. Note: In c octal number starts with 0 and hexadecimal number starts with 0x. (16) What will be output if you will compile and #define message "union is\ power of c" printf("%s",message); (a) union is power of c (b) union ispower of c (c) union is Power of c (d) Compiler error (e) None of these Answer: (b) Page 20

If you want to write macro constant in new line the end with the character \. (17) What will be output if you will compile and #define call(x) #x printf("%s",call(c/c++)); (a)c (b)c++ (c)#c/c++ (d)c/c++ (e)compiler error Answer: (d) # is string operator. It converts the macro function call argument in the string. First see the intermediate file: test.c 1: test.c 2: void main(){ test.c 3: printf("%s","c/c++"); test.c 4: test.c 4: test.c 5: Page 21

It is clear macro call is replaced by its argument in the string format. (18) What will be output if you will compile and if(printf("cquestionbank")) printf("i know c"); else printf("i know c++"); (a) I know c (b) I know c++ (c) cquestionbanki know c (d) cquestionbanki know c++ (e) Compiler error Answer: (c) Return type of printf function is integer which returns number of character it prints including blank spaces. So printf function inside if condition will return 13. In if condition any non- zero number means true so else part will not execute. (19) What will be output if you will compile and Page 22

int i=10; static int x=i; if(x==i) printf("equal"); else if(x>i) printf("greater than"); else printf("less than"); (a) Equal (b) Greater than (c) Less than (d) Compiler error (e) None of above Answer: (d) Static variables are load time entity while auto variables are run time entity. We cannot initialize any load time variable by the run time variable. In this example i is run time variable while x is load time variable. (20) What will be output if you will compile and Page 23

printf("%s", DATE ); (a) Current system date (b) Current system date with time (c) null (d) Compiler error (e) None of these Answer: (a) DATE is global identifier which returns current system date. (21) What will be output if you will compile and void start(); void end(); #pragma startup start #pragma exit end int static i; printf("\nmain function: %d",++i); Page 24

void start(){ printf("\nstart function: %d",++i); void end(){ printf("\nend function: %d",++i); (a) main function: 2 start function: 1 end function:3 (b) start function: 1 main function: 2 end function:3 (c) main function: 2 end function:3 start function: 1 (d) Compiler error (e) None of these Answer: (b) Every c program start with main function and terminate with null statement. But #pragma startup can call function just before main function and #pragma exit Page 25