FOCP–Introduction to C (Unit 4)–2 marks with answers

FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

UNIT IV

INTRODUCTION TO C


Overview of C – Constants, Variables and Data Types – Operators and Expressions– Managing Input and Output operators – Decision Making - Branching and Looping.


2 MARKS


1. What are the different data types available in ‘C’?

There are four basic data types available in ‘C’.

1. int

2. float

3. char

4. double


2. What are Keywords?

Keywords are certain reserved words that have standard and pre-defined meaning in

‘C’. These keywords can be used only for their intended purpose.


3. What is an Operator and Operand?

An operator is a symbol that specifies an operation to be performed on operands.

Example: *, +, -, / are called arithmetic operators.

The data items that operators act upon are called operands.

Example: a+b; In this statement a and b are called operands.


4. What is Ternary operators or Conditional operators?

Ternary operators is a conditional operator with symbols ? and :

Syntax: variable = exp1 ? exp2 : exp3

If the exp1 is true variable takes value of exp2. If the exp2 is false, variable takes the value of exp3.


5. What are the Bitwise operators available in ‘C’?

& - Bitwise AND

| - Bitwise OR

~ - One’s Complement

>> - Right shift

<< - Left shift

^ - Bitwise XOR are called bit field operators

Example: k=~j; where ~ take one’s complement of j and the result is stored in k.


6. What are the logical operators available in ‘C’?

The logical operators available in ‘C’ are

&& - Logical AND

|| - Logical OR

! - Logical NOT


7. What is the difference between Logical AND and Bitwise AND?

Logical AND (&&): Only used in conjunction with two expressions, to test more than one condition. If both the conditions are true the returns 1. If false then return

0.

AND (&): Only used in Bitwise manipulation. It is a unary operator.


8. What is the difference between ‘=’ and ‘==’ operator?

Where = is an assignment operator and == is a relational operator.

Example:

while (i=5) is an infinite loop because it is a non zero value and while (i==5) is true only when i=5.


9. What is type casting?

Type casting is the process of converting the value of an expression to a particular data type.

Example:

int x,y;

c = (float) x/y; where a and y are defined as integers. Then the result of x/y is converted into float.


10. What is conversion specification?

The conversion specifications are used to accept or display the data using the

INPUT/OUTPUT statements.


11. What is the difference between ‘a’ and “a”?

‘a’ is a character constant and “a” is a string.


12. What is the difference between if and while statement?

if

while

(i) It is a conditional statement

(i) It is a loop control statement

(ii) If the condition is true, it executes

some statements.

(ii) Executes the statements within the

while block if the condition is true.

(iii) If the condition is false then it stops the execution the statements.

(iii) If the condition is false the control is

transferred to the next statement of the loop.


13. What is the difference between while loop and do…while loop?

In the while loop the condition is first executed. If the condition is true then it executes the body of the loop. When the condition is false it comes of the loop. In the do…while loop first the statement is executed and then the condition is checked. The do…while loop will execute at least one time even though the condition is false at the very first time.


14. What is a Modulo Operator?

‘%’ is modulo operator. It gives the remainder of an integer division

Example:

a=17, b=6. Then c=%b gives 5.


15. How many bytes are occupied by the int, char, float, long int and double?

int - 2 Bytes

char - 1 Byte float - 4 Bytes long int - 4 Bytes double - 8 Bytes


16. What are the types of I/O statements available in ‘C’?

There are two types of I/O statements available in ‘C’.

· Formatted I/O Statements

· Unformatted I/O Statements


17. What is the difference between ++a and a++?

++a means do the increment before the operation (pre increment) a++ means do the increment after the operation (post increment) Example:

a=5;

x=a++; /* assign x=5*/

y=a; /*now y assigns y=6*/

x=++a; /*assigns x=7*/


18. What is a String?

String is an array of characters.


19. What is a global variable?

The global variable is a variable that is declared outside of all the functions. The

global variable is stored in memory, the default value is zero. Scope of this variable is available in all the functions. Life as long as the program’s execution doesn’t come to an end.


20. What are the Escape Sequences present in ‘C’

\n - New Line

\b - Backspace

\t - Form feed

\’ - Single quote

\\ - Backspace

\t - Tab

\r - Carriage return

\a - Alert

\” - Double quotes


21. Construct an infinite loop using while?

while (1)

{

}

Here 1 is a non zero, value so the condition is always true. So it is an infinite loop.


22. What will happen when you access the array more than its dimension?

When you access the array more than its dimensions some garbage value is

stored in the array.


23. Write the limitations of getchar( ) and sacnf( ) functions for reading strings (JAN 2009)

getchar( )

To read a single character from stdin, then getchar() is the appropriate.

scanf( )

scanf( ) allows to read more than just a single character at a time.


24. What is the difference between scanf() and gets() function?

In scanf() when there is a blank was typed, the scanf() assumes that it is an end.

gets() assumes the enter key as end. That is gets() gets a new line (\n) terminated string of characters from the keyboard and replaces the ‘\n’ with ‘\0’.


25. What is a Structure?

Structure is a group name in which dissimilar data’s are grouped together.


26. What is meant by Control String in Input/Output Statements?

Control Statements contains the format code characters, specifies the type of data that the user accessed within the Input/Output statements.


27. What is Union?

Union is a group name used to define dissimilar data types. The union occupies only the maximum byte of the data type. If you declare integer and character, then the union occupies only 2 bytes, whereas structure occupies only 3 bytes.


28. What is the output of the programs given below?

main() main()

{ {

float a; float a;

int x=6, y=4; int x=6, y=4;

a=x\y; a=(float) x\y;

printf(“Value of a=%f”, a); printf(“Value of a=%f”,a);

} }

Output: Output:

1. 1.500000


29. Declare the Structure with an example?

struct name

{

char name[10];

int age;

float salary;

} e1, e2;


30. Declare the Union with an example?

union name

{

char name[10];

int age;

float salary;

} e1, e2;


31. What is the output of the following program when, the name given with spaces?

main()

{

char name[50]; printf(“\n name\n”); scanf(“%s, name); printf(“%s”,name);

}

Output:

Lachi (It only accepts the data upto the spaces)


32. What is the difference between while(a) and while(!a)?

while(a) means while(a!=0)

while(!a) means while(a==0)


33. Why we don’t use the symbol ‘&’ symbol, while reading a String through scanf()?

The ‘&’ is not used in scanf() while reading string, because the character variable

itself specifies as a base address.

Example: name, &name[0] both the declarations are same.


34. What is the difference between static and auto storage classes?

Static

Auto

Storage

Initial value

Scope

Life

Memory

Zero

Local to the block in which the variables is defined

Value of the variable persists

between different function calls.

Memory

Garbage value

Local to the block in which the variable is defined.

The block in which the

variable is defined.


35. What is the output of the program?

main()

increment()

{

{

increment();

static int i=1;

increment();

printf(“%d\n”,i)

increment();

}

i=i+1;

}

OUTPUT:

1 2

3


36. Why header files are included in ‘C’ programming?

· This section is used to include the function definitions used in the program.

· Each header file has ‘h’ extension and include using ’# include’ directive at the beginning of a program.


37. List out some of the rules used for ‘C’ programming.

· All statements should be written in lower case letters. Upper case letters are only for

symbolic constants.

· Blank spaces may be inserted between the words. This improves the readability of statements.

· It is a free-form language; we can write statements anywhere between ‘{‘ and ‘}’. a = b + c;

d = b*c;

(or)

a = b+c; d = b*c;

· Opening and closing braces should be balanced.


38. Define delimiters in ‘C’.

Delimiters

Use

:

;

( ) [ ]

{ }

#

,

Colon

Semicolon Parenthesis Square Bracket Curly Brace Hash

Comma

Useful for label

Terminates Statement

Used in expression and functions

Used for array declaration Scope of statement Preprocessor directive Variable Separator


39. What do you mean by variables in ‘C’?

· A variable is a data name used for storing a data value.

· Can be assigned different values at different times during program execution.

· Can be chosen by programmer in a meaningful way so as to reflect its function in the program.

· Some examples are: Sum percent_1 class_total


40. List the difference between float and double datatype.

S No

Float

Double Float / Double

1

2

3

4

Occupies 4 bytes in memory

Range : 3.4 e-38 to 3.8e+38

Format Specifier: % f

Example : float a;

Occupies 8 bytes in memory

Range : 1.7 e-308 to 1.7e+308

Format Specifier: % lf

Example : double y;

There exists long double having a range of 3.4 e -4932 to 3.4 e +4932 and occupies 10 bytes in memory.

Example: long double k;


41. Differentiate break and continue statement

S No

break

continue

1

2

3

Exits from current block / loop

Control passes to next statement

Terminates the program

Loop takes next iteration

Control passes to beginning of loop

Never terminates the program


42. List the types of operators.

S No

Operators Types

Symbolic Representation

1

2

3

4

5

6

7

8

Arithmetic operators

Relational operators

Logical operators

Increment and Decrement operators

Assignment operators Bitwise operators Comma operator Conditional operator

= , - , * , / and %

> , < , == , >=, <= and !=

&& , || and !

++ and –

= , + = , - = , * = , / = , ^ = , ; = , & =

& , | , ^ , >> , << , and ~

,

? :


43. Distinguish between while..do and do..while statement in C. (JAN 2009)

While..DO                                                    DO..while

(i) Executes the statements within the

while block if only the condition is true.

(i) Executes the statements within the

while block at least once.

(ii) The condition is checked at the

starting of the loop

(ii) The condition is checked at the end of

the loop


44. Compare switch( ) and nestedif statement.

S No

switch( ) case

nested if

1

2

3

4

Test for equality ie., only constant

values are applicable.

No two case statements in same switch.

Character constants are automatically converted to integers.

In switch( ) case statement nested if can be used.

It can equate relational (or)

logical expressions.

Same conditions may be repeated for a number of times.

Character constants are automatically converted to integers.

In nested if statement switch case can be used.


45. Distinguish Increment and Decrement operators.

S No

Increment ++

Decrement --

1

2

3

4

Adds one to its operand

Equivalent x = x + 1

Either follow or precede operand

Example : ++x; x++;

Subtracts one from its operand

Equivalent x = x - 1

Either follow or precede operand

Example : --x; x--;


46. Give the syntax for the ‘for’ loop statement

for (Initialize counter; Test condition; Increment / Decrement)

{

statements;

}

· Initialization counter sets the loop to an initial value. This statement is executed only once.

· The test condition is a relational expression that determines the number of iterations desired or it determines when to exit from the loop. The ‘for’ loop continues to execute as long as conditional test is satisfied. When condition becomes false, the control of program exists the body of the ‘for’ loop and executes next statement after the body of the loop.

· The increment / decrement parameter decides how to make changes in the loop.

· The body of the loop may contain either a single statement or multiple statements.


47. What is the use of sizeof( ) operator?

· The sizeof ( ) operator gives the bytes occupied by a variable.

· No of bytes occupied varies from variable to variable depending upon its data types.

Example:

int x,y;

printf(“%d”,sizeof(x));

Output:

2


48. What is a loop control statement?

Many tasks done with the help of a computer are repetitive in nature. Such tasks

can be done with loop control statements.


49. What are global variable in ‘C’?

· This section declares some variables that are used in more than one function. such

variable are called as global variables.

· It should be declared outside all functions.


50. Write a program to swap the values of two variables (without temporary variable).

#include <stdio.h>

#include <conio.h>

void main( )

{

int a =5; b = 10;

clrscr( );

prinf(“Before swapping a = %d b = %d “, a , b);

a = a + b; B = a – b;

a = a – b;

prinf(“After swapping a = %d b = %d”, a,b);

getch( );

}

Output:

Before swapping a = 5 b = 10

After swapping a = 10 b = 5


51. Write short notes about main ( ) function in ’C’ program. (MAY 2009)

· Every C program must have main ( ) function.

· All functions in C, has to end with ‘( )’ parenthesis.

· It is a starting point of all ‘C’ programs.

· The program execution starts from the opening brace ‘{‘ and ends with closing brace

‘}’, within which executable part of the program exists.