Tuesday 22 March 2016

MCQ - CPU


1) Who is father of C Language?




D.  E. F. Codde

2) C Language developed at _________?




D. Cambridge University in 1972

3) Output of follwing code 
  #include <stdio.h>
  void main() 
  {
             int x = 5;  
             if (x < 1); 
                 printf("Hello");
}

4) Output of following code

#include <stdio.h>
        void main()
        {
            double ch;
            printf("enter a value btw 1 to 2:");
            scanf("%lf", &ch);
            switch (ch)
            {
            case 1:
                printf("1");
                break;
            case 2:
                printf("2");
                break;
            }
        }


5) Output of following code

    #include <stdio.h>
    void main()
   {
            int ch;
            printf("enter a value btw 1 to 2:");
            scanf("%d", &ch);
            switch (ch, ch + 1)
            {
            case 1:
               printf("1\n");
                break;
            case 2:
                printf("2");
                break;
            }
        }


6) Which of the following operator takes only integer operands?
A. +
B. *
C. /
D. %
E. None of these


7)  Output:
void main()
{
    int i=0, j=1, k=2,m;
    m = i++|| j++ || k++;
    printf("%d %d %d %d",m,i,j,k);


8) Output:
void main()
{
      int i=10;
      i = !i>14;
      printf("i=%d", i);
}
 
9) Output:
void main()
{
      int a, b, c, d;
      a = 3;
      b = 5;
      c = a, b;
      d = (a, b);
      printf("c=%d d=%d", c, d);
}
10) Output:
void main()
{
    int z, x=5, y= -10, a=4, b=2;
    z = x++ - --y*b/a;
    printf("%d",z);
}
 



Monday 7 March 2016

Programming Definition - CPU


1) Write a  program to display following pattern (Rectangle, arrow, rectange).
2) Write a program to display the following pattern.
x=a/b-c;
3) Write a program to convert fahrenheit to celsius and celsius to fahrenheit.
F=9C/5 + 32;
4) Write a program to display the equation of line in a form of : ax + by = c
value of a, b and c is taken from user
5) Write a program to take the input in form of rupees and paise and convert it into paise
(15.29 -= 1529 paise)
6) The price of one kg of rice is 15.25 and one kg of sugar is 20. Get the number of KG from user and display the value in following form
****List of Itemes***** 
Item                         Price
Rice                         15.25
Sugar                       20
Total                        25.25
7) Write a program to Swapping of two numbers
8) Write a program to find the simple interest
9) Write a program to read 5 subjects marks and display the percentage
10) Write a program to calculate the gross salary of employee.
11) Write a program to check number is odd or even
12) Write a program to check number is postive, negative or zero
13) Write a program to find greater number from two numbers
14) Write a program to find greater number from three numbers
15) Write a program to find Electricity bill
16) Write a program to swap the value of  two variables without using third.
17) Write a program to check year is leap year or not.
18) Write a program to find maximum number between two numbers using ternary operator
19) Write a program to perform division using shift right
20) Write a program to perform multiplication using shift left
21) Write a program to print ASCII value of a character
22) Write a program to convert lower case letter into upper case
23) Write a program to check person is eligible for vote or not
24) Write a program to display the examination result
if avg is above 80 then distinction
if avg is less than 80 and greater than 70 then first class
if avg is less than 70 and greater than 60 then second class
if avg is less than 60 and greater than 50 then pass class
if avg is less than 50 then fail
25) Write a program to display the total amount after purchasing the clothes with consideration of following details
if cloth is from mill then discount is 0%, 5%, 10% and 15% if amount is between 0-100,100-200,200-300 and above 300
if cloth is from handloom then discount is 5%,10%,15% and 20%
26) Write a program that will read the value of x and evaluate the following function
y = 4x + 100 for x<40
y=300 for x=40
y=4.5x + 150 for x<40
27) Write a program to check candidate is eligible for admission or not based on following condition
marks in maths >=60
marks in phy>= 50
marks in chem>=40
total in all 3 >=200
or total in maths & phy >=150
28) Write a program to read the value of 4 variables a,b,c and d and find the ratio of (a-b)/(c-d) and print result only if c-d is not equal to zero
29)Write a program to check employee is eligible for loan or not.En employee can apply for a loan at the beginning of every six months but he will be get the amount according to the following company rule
1) cannot get more than two loan
2) maximum permissible total loan is limited to 50000
3 loan amount need to give
if loan1 is zero then check the loan2 and loan 3 not more than 50000
if loan1 is zero check loan 2 is zero if loan 2 is not zero then check total of loan 2 and loan3 is greater than max than eligible amount is max - total
30) Write a program to print first 10 numbers
31) Write a program to print sum of first 10 numbers
32) Write a program to print 20 horizontal asterisks(*)
33) Write a program to print the sum of numbers from m to n
34) Write a program to print largest number from six number using ternery operator
35) Write a program to count total number of positive, negative and zero until -1 is entered,
36) Write a program to print day using switch case.
37) Write a program to create calculator using switch case.

Wednesday 2 March 2016

MCQ - C language

Give your answer with explaination:
 
1) What will be output if you will compile and execute the following c code?


#define x 5+2
void main(){
    int i;
    i=x*x*x;
    printf("%d",i);
}

2) What will be output if you will compile and execute the following c code?

void main(){
char c=125;
    c=c+10;
    printf("%d",c);
}

3) What will be output if you will compile and execute the following c code?

void main(){
   float a=5.2;
  if(a==5.2)
     printf("Equal");
  else if(a<5.2)
     printf("Less than");
  else
     printf("Greater than");
}

4) What will be output if you will compile and execute the following c code?


void main(){
  int i=4,x;
  x=++i + ++i + ++i;
  printf("%d",x);
}

5)What will be output if you will compile and execute the following c code?


void main(){
  int a=10;
  printf("%d %d %d",a,a++,++a);
}
6)What will be output if you will compile and execute the following c code?


void main(){
   char *str="Hello world";
   printf("%d",printf("%s",str));
}
7) What will be output if you will compile and execute the following c code?


#define call(x,y) x##y
void main(){
int x=5,y=10,xy=20;
printf("%d",xy+call(x,y));
}
8) What will be output if you will compile and execute the following c code?


#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}




Operator in C - Material CPU



Explain different operators of C language with example.
Answer:

Operator: It is a symbol which is used to tell the compiler which mathematical or logical operations are to be applied on operands (data).
C language has following 8 different types of operators.

1)    Arithmetic operators
2)    Relational operators
3)    Logical operators
4)    Assignment operators
5)    Increment or decrement operators
6)    Bit wise operators
7)    Ternary (Conditional) operators
8)    Other operators

Arithmetic operators:
It is used to perform arithmetic operation like addition, subtraction, division, multiplication and modules.


Operator Name
Meaning
+
Addition
-
Subtraction
/
Division
*
Multiplication
%
Modulus

 
            Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=10,b=20;
clrscr();
c=a+b;
printf(“%d”,c);
getch();
}
Relational Operator:
These operators are used to find the relationship between two operands.
The answers of these operators are always either true or false.
Consider a = 10 and b = 5 for following table.

Operator

Meaning
Answer
a==b
Check the value of a and b are same or not
False
a>b
Check the value of a is greater than the value of b
True
a<b
Check the value of a is less than the value of b
False
a>=b
Check the value of a is greater than or equals to the value of b
True
a<=b
Check the value of a is less than or equals to the value of b
False
a!=b
Check the value of a is not equal to the value of b
True

Logical Operators:
Logical operators are used to combine the more than one condition.
C language has 3 different types of logical operators.
The output of logical operators is always either true or false.
Consider a = 10, b = 5 and c = 3 for following table.

Operator
Example
Output
&& - Logical and
(a > b) && (a>c)
True
|| - Logical or
(b > a) || (a>b)
True
! – Logical not
!(a>b)
False


Assignment Operators:
Assignment operator is used to assign the value to variable.
To assign the value to variable following syntax is used.

Syntax:
            <Variable name> = <expression>;

Expression can be any constant, variable name or any valid expression.

Example:      a = 10;
C language also support shorthand operator.
Shorthand operator can be used with statement having following form:

            <Variable name> = <variable name> <operator> <expression>;

Here both variable names must be same.
This form is converted into following form using shorthand operator.

            <Variable name> <operator> = <expression>;

Assignment operator
Shorthand
a = a + 5;
a+=5;
a = a – 5;
a-=5;
a = a * 10;
a*=10;
a = a / 20;
a/=20;


Increment or Decrement operator:
It is unary operator that means it requires only one operator.
Operator can be written either after or before the operand.


Operator
Meaning
++
Increment operator
--
Decrement operator

Increment operator is used to increase the value of variable by one and decrement operators is used to decrease the value of variable bye one only.
If increment operator is placed after variable then it is known as post increment and if increment operator is placed before variable then it is known as pre increment.

            Example:
                        #include<stdio.h>
                        #include<conio.h>
                        void main()
                     {
                                    int a1,b1;
                                    a1=10;
                                    b1=a1++; // post increment
                                    printf(“%d”,b1);
                                    printf(“%d”,a1);
                                    getch();
                        }                      

Bitwise operator:
C language also supports some operators which can perform operation on bit level. So using this operator we can perform operation on 0’s and 1’s bit.
Consider a = 10 (1010) and b = 5 (0101).

Operator
Example
Output
& - Bitwise AND
c = a & b
0
| - Bitwise OR
c = a | b
15
^ - Bitwise Exclusive OR (XOR)
c = a ^ b
15
<< - Left Shift
c = a << 1
20
>> - Right Shift
c = a >> 1
5
~ - Bitwise 1’s complement
c = ~a
-11

For Bitwise and (&) operator if both bits are 1 then only answer is 1.
For Bitwise or(|) operator if one the bit is 1 then answer is 1 and if both bits are 0 then only answer is 0.
For Bitwise exclusive or (XOR) operator if both bits are different then only answer is 1 other wise answer is 0.
In left shift operator if we shift one bit to left then result will be in multiplication of 2, if we shift 2 bits in left side then result will be in multiplication of 4 and so on.

For Example:
a=10; if we write a<<1 then answer is 20 as below.

           
0
0
0
0
1
0
1
0


0
0
0
1
0
1
0
0



           
Example:
                        #include<stdio.h>
                        #include<conio.h>
                        void main()
                        {
                                    int a1,b1;
                                    a1=10;
                                    b1=a1<<1;
                                    printf(“%d”,b1);
                                    b1=a1>>1;
                                    printf(“%d”,b1);
                                    getch();
                        }          

Conditional operator:
It is also known as ternary operator.
It works like if else statement.



Syntax:
<Condition> ? <Statement 1> : <statement 2>;

 
 





If condition becomes true then statement 1 will be executed.
If condition becomes false then statement 2 will be executed
           
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int max,a,b;
clrscr();
a=10, b=20;
max = (a>b)? a : b;
printf(“maximum value is %d”,max);
getch();
}

Other operators:
In that some special operators like comma operator, dot (.) operator, arrow (->) operator and sizeof operator are used.
Comma operator is used to declare multiple variable, expression and also used in printf() or scanf() function.
Dot (.) operator is used to access the member of structure using structure variable.
Arrow (->) operator is used to access the member of structure using pointer variable.
Sizeof() operator is used to find the no of bytes occupied by specified data type or variable.

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“%d”,sizeof(a)); // size of integer is 2 bytes so output is 2
getch();
}