Sunday 27 August 2017

APTITUDE QUESTION



1) Which operator is used to get the "content of a variable pointed to by a pointer"?

1.Dot Operator (.)
2.Address of Operator (&)
3.Indirection or De-reference Operator (*)
4.Arrow Operator (→)
Answer & Explanation:
Correct answer: 3
Indirection or De-reference Operator (*)


2) What will be the type of a pointer, which is pointing to an integer variable?

1.int *
2.long int *
3.char *
4.unsigned *
Answer & Explanation:
Correct answer: 1
int *
 To store the address of an integer variable, we need to declare an integer pointer (int *).


3) If ptr is a pointer to one dimensional array, which statement will return the value of 3rd element?

1.(ptr+3)
2.*(ptr+3)
3.(&ptr+3)
4.*ptr+3
Answer & Explanation:
Correct answer: 2
*(ptr+3)
 ptr will point to the address of one-dimensional array and * (indirection or de-reference operator) returns the value of variable pointed by a pointer, thus *(ptr+3) will return the value of 3rd element of array.


 4) Consider the given declaration:

int* ptr1,ptr2;
What is the type of ptr2 here?

1.int * (Integer pointer)
2.void
3.void * (void pointer)
4.int (integer)
Answer & Explanation:
Correct answer: 2
int (integer)
 Here, compiler will ignore the pointer asterisk, when carrying a type over to multiple declarations. If we split the declarations of ptr1 and ptr2 into multiple declarations, compiler will consider them like:
 int *ptr1;
int ptr2;
Thus, ptr is not int*, it is int type variable.


5) What will be the correct way to declare two integer pointers using single line declaration?

1.int *ptr1,*ptr2;
2.int* ptr1,ptr2;
3.int * ptr1, ptr2;
4.All of the above
Answer & Explanation:
Correct answer: 1
int *ptr1,*ptr2;
 It is possible to declare multiple pointers in a single line declaration, use asterisk (*) before the pointer names.


6) Consider the following statement:

int arr[5],*ptr;
How to initialize ptr with the address of array arr?

1.ptr = arr;
2.ptr = &arr[0];
3.ptr = &arr;
4.Both 1) and 2)
Answer & Explanation:
Correct answer: 4
Both 1 and 2
 A pointer to an array of integers can be initialized using array name (arr) or base address of first array element .



 



Sunday 6 August 2017

WALK-IN-INTERVIEW




#1: Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
Answer: b
Explanation: space, comma and $ cannot be used in a variable name.
#2: What is the output of this C code?
#include
int main()
{
    printf("Hello World! %d \n", x);
    return 0;
}
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
 Answer: c
Explanation: It results in an error since x is used without declaring the variable x.
#3: What will happen if the below program is executed?
#include
int main()
{
    int main = 3;
    printf("%d", main);
    return 0;
}
a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
Answer: c
Explanation: A C program can have same function name and same variable name.
#4: What is the output of this C code?
#include 
int main()
{
   char chr;
   chr = 128;
   printf("%d\n", chr);
   return 0;
}
a) 128
b) -128
c) Depends on the compiler
d) None of the mention

Answer: b
Explanation: signed char will be a negative number.
#5: What is the output of this C code?
#include
int main()
{
    char *p[1] = {"hello"};
    printf("%s", (p)[0]);
    return 0;
}
a) Compile time error
b) Undefined behaviour
c) hello
d) None of the mentioned
Answer: c
Explanation: None
#6: What is the output of this C code?
#include
int main()
{
    printf("crazyfor\code\n");
    return 0;
}
a) crazyforcode
b) crazyforcode
c) codeyfor
d) crazyfor
Answer: c
Explanation: r is carriage return and moves the cursor back. sanfo is replaced by class


Friday 4 August 2017

CHALLENGE THIS!!!!!!!





Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not.

   B(10,30)
           / \
          /   \
         /     \
        /   P   \      P'
       /         \
A(0,0) ----------- C(20,0)
The program needs to read the values of three coordinates

A(x1,y1)
B(x2,y2)
C(x3,y3)
as well as another coordinate P(x,y) and determine whether this point is inside a triangle formed from the 3 point above.

Solution:
Let the coordinates of three corners be (x1, y1), (x2, y2) and (x3, y3). And coordinates of the given point P be (x, y)

1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2
2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1.
3) Calculate area of the triangle PBC. Let this area be A2.
4) Calculate area of the triangle PAC. Let this area be A3.
5) If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.

Implementation

float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
 
/* A function to check whether point P(x, y) lies inside the triangle  */
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);
 
   /* Calculate area of triangle PBC */
   float A1 = area (x, y, x2, y2, x3, y3);
 
   /* Calculate area of triangle PAC */
   float A2 = area (x1, y1, x, y, x3, y3);
 
   /* Calculate area of triangle PAB */ 
   float A3 = area (x1, y1, x2, y2, x, y);
   
   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 +A3);

}

Saturday 15 July 2017

PRINT ASCII TABLE/ASCII CHART

#include
int main()
{
unsigned char count;
for(count=32;count<255 count="" p="">{
printf(" %3d - %c",count,count);
if(count % 6==0)
printf("\n");
}
return 0;

}
IT REALLY  WORKS....

Tuesday 11 July 2017

INTERESTING FACT ON C


Below is one of the interesting facts about C programming:

1) The case labels of a switch statement can occur inside if-else statement

#include

int main()
{
    int a = 2, b = 2;
    switch(a)
    {
    case 1:
        ;

        if (b==5)
        {
        case 2:
            printf("GeeksforGeeks");
        }
    else case 3:
    {

    }
    }
}
Run on IDE
Output :GeeksforGeeks

Monday 10 July 2017

FACTORIAL


#include
int main()
{
    int n, i;
    unsigned long long factorial = 1;

    printf("Enter an integer: ");
    scanf("%d",&n);

    // show error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");

    else
    {
        for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
    }

    return 0;
}

Tuesday 4 July 2017



               
1.In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
A.            "I am a boy\r\n\0"
B.            "I am a boy\r\0"
C.            "I am a boy\n\0"
D.            "I am a boy"
TRY  IT YOURSELF


Answer: Option C
Explanation:
Declaration: char *fgets(char *s, int n, FILE *stream);
fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.

Therefore the  string str  contain “ I am a boy\n\0”.