Showing posts with label c question. Show all posts
Showing posts with label c question. Show all posts

Pointer C Questions 2012

5 comments Posted by Unknown at 19:39



1. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char arr[7]="Network";
printf("%s",arr);
}
Explanation:
Size of a character array should one greater than total number of characters in any string which it stores. In c every string has one terminating null character. This represents end of the string.So in the string “Network” , there are 8 characters and they are ‘N’,’e’,’t’,’w’,’o’,’r’,’k’ and ‘\0’. Size of array arr is seven. So array arr will store only first sevent characters and it will note store null character.
As we know %s in prinf statement prints stream of characters until it doesn’t get first null character. Since array arr has not stored any null character so it will print garbage value.

2.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    char arr[11]="The African Queen";
    printf("%s",arr);
}
Explanation:
Size of any character array cannot be less than the number of characters in any string which it has assigned. Size of an array can be equal (excluding null character) or greater than but never less than.

3.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    int const SIZE=5;
    int expr;
    double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
    expr=1|2|3|4;
    printf("%f",value[expr]);
}
Explanation:
Size of any array in c cannot be constantan variable.

4.What will be output when you will execute following c code?
#include<stdio.h>
enum power{
    Dalai,
    Vladimir=3,
    Barack,
    Hillary
};
void main(){
    float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
    enum power p=Barack;
    printf("%0.f",leader[p>>1+1]);
}
Explanation:
Size of an array can be enum constantan.
Value of enum constant Barack will equal to Vladimir + 1 = 3 +1 = 4
So, value of enum variable p  = 4
leader[p >> 1 +1]
= leader[4 >> 1+1]
=leader[4 >> 2]   //+ operator enjoy higher precedence than >> operator.
=leader[1]  //4>>2 = (4 / (2^2) = 4/4 = 1
=2

5.What will be output when you will execute following c code?
#include<stdio.h>
#define var 3
void main(){
    char *cricket[var+~0]={"clarke","kallis"};
    char *ptr=cricket[1+~0];
    printf("%c",*++ptr);
}
Explanation:
In the expression of size of an array can have micro constant.
var +~0 = 3 + ~0 = 3 + (-1)  = 2
Let’s assume string “clarke” and “kallis” has stored at memory address 100 and 500 respectively as shown in the following figure:
For string “clarke”:
For string “kallis”:
In this program cricket is array of character’s pointer of size 2. So array cricket will keep the memory address of first character of both strings i.e. content of array cricket is:
cricket[2] = {100,500}
ptr is character pointer which is pointing to the fist element of array cricket. So, ptr = 100
Now consider on *++ptr
Since ptr = 100 so after ++ptr , ptr = 101
*(++ptr) = *(101) = content of memory address 101. From above figure it is clear that character is l.

6.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
    printf("%o",data[0][2][1]);
}
Explanation:
%o in printf statement is used to print number in the octal format.

7.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    short num[3][2]={3,6,9,12,15,18};
    printf("%d  %d",*(num+1)[1],**(num+2));
}
Explanation:
*(num+1)[1]=*(*((num+1)+1))=*(*(num+2))=*(num[2])=num[2][0]=15And**(num+2)=*(num[2]+0)=num[2][0]=15
8.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    char *ptr="cquestionbank";
    printf("%d",-3[ptr]);
}
Explanation:
-3[ptr]=-*(3+ptr)=-*(ptr+3)
=-ptr[3]
=-103  //ASCII value of character ‘e’ is 103

9.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    long  myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
    printf("%ld\t",myarr[1][2]);
    printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
    printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);  
}
Explanation:
Think yourself.

10.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    int array[2][3]={5,10,15,20,25,30};
    int (*ptr)[2][3]=&array;
    printf("%d\t",***ptr);
    printf("%d\t",***(ptr+1));
    printf("%d\t",**(*ptr+1));
    printf("%d\t",*(*(*ptr+1)+2));
}
Explanation:
ptr is pointer to two dimension array.
***ptr
=***&array  //ptr = &array
=**array //* and & always cancel to each other
=*arr[0]  // *array = *(array +0) = array[0]
=array[0][0]
= 5
Rests think yourself.

11.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    static int a=2,b=4,c=8;
    static int *arr1[2]={&a,&b};
    static int *arr2[2]={&b,&c};
    int* (*arr[2])[2]={&arr1,&arr2};
    printf("%d %d\t",*(*arr[0])[1],  *(*(**(arr+1)+1)));
}
Explanation:
Consider on the following expression:
*(*arr[0])[1]
=*(*&arr1)[1]  //arr[0] = &arr1
=*arr1[1]   //* and & always cancel to each other
=*&b
=b
=4
Consider on following expression:
*(*(**(arr+1)+1))
= *(*(*arr[1]+1))  //*(arr+1) = arr[1]
= *(*(*&arr2+1))  //arr[1] = &arr2
=*(*(arr2+1))  //*&arr2 = arr2
=*(arr2[1])  //*(arr2+1) = arr2[1]
=  *&c    //arr2[1] = &c
=  c
= 8

12.What will be output when you will execute following c code?
#include<stdio.h>
#include<math.h>
double myfun(double);
void main(){
    double(*array[3])(double);
    array[0]=exp;
    array[1]=sqrt;
    array[2]=myfun;
    printf("%.1f\t",(*array)((*array[2])((**(array+1))(4))));  
}
double myfun(double d){
       d-=1;
       return d;
}
Explanation:
array is array of pointer to such function which parameter is double type data and return type is double.
Consider on following expression:
(*array)((*array[2])((**(array+1))(4)))
= (*array)((*array[2])((*array[1])(4)))
//*(array+1) = array[1]
= (*array)((*array[2])(sqrt(4))))
//array[1] = address of sqrt function
= (*array)((*array[2])(2.000000)))
= (*array)(myfun(2.000000)))
// array[2] = address of myfunc function
=(*array)(1.000000)
=array[0](1.000000)
=exp(1.000000)

13.What will be output when you will execute following c code?
#include<stdio.h>
typedef struct{
    char *name;
    double salary;
}job;
void main(){
    static job a={"TCS",15000.0};
    static job b={"IBM",25000.0};
    static job c={"Google",35000.0};
    int x=5;
    job * arr[3]={&a,&b,&c};
    printf("%s  %f\t",(3,x>>5-4)[*arr]);
}
double myfun(double d){
       d-=1;
       return d;
}
Explanation:
(3,5>>5-4)[*arr]
=(3,5>>5-4)[*arr] //x=5
= (3,5>>1)[*arr] //- operator enjoy higher precedence than >>
= (3,2)[*arr]  //5>>1 = 5/(2^1) = 5 /2 = 2
= 2[*arr]  //In c comma is also operator.
= *(2 + *arr)
= *(*arr + 2)
=*arr[2]
=*(&c) //arr[2] = &c
=c   // *  and & always cancel to each other.
So,
printf("%s  %f\t",c);
=> printf("%s  %f\t", "Google",35000.0);

14.What will be output when you will execute following c code?
#include<stdio.h>
union group{
    char xarr[2][2];
    char yarr[4];
};
void main(){
    union group x={'A','B','C','D'};
    printf("%c",x.xarr[x.yarr[2]-67][x.yarr[3]-67]);
}
Explanation:
In union all member variables share common memory space.
So union member variable, array xarray will look like:
{
{‘A’,’B’},
{‘C’,’D’}
}
And union member variable, array yarray will look like:
{
{‘A’,’B’,’C’,’D’}
}
x.xarr[x.yarr[2]-67][x.yarr[3]-67]
= x.xarr[‘C’-67][‘D’-67]
= x.xarr[67-67][68-67]
//ASCII value of ‘C’ is 67 and ‘D’ is 68.
x.xarr[0][1]
=’B’

15.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    int a=5,b=10,c=15;
    int *arr[3]={&a,&b,&c};
    printf("%d",*arr[*arr[1]-8]);
}
Explanation:
Member of an array cannot be address of auto variable because array gets memory at load time while auto variable gets memory at run time.
16.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    int arr[][3]={{1,2},{3,4,5},{5}};
    printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}
Explanation:
If we will not write size of first member of any array at the time of declaration then size of the first dimension is max elements in the initialization of array of that dimension.
So, size of first dimension in above question is 3.
So size of array = (size of int) * (total number of elements) = 2 *(3*3) = 18
Default initial value of rest elements are zero.  So above array will look like:
{
{1,2,0}
{3,4,5},
{5,0,0}
}        

17.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    int xxx[10]={5};
    printf("%d %d",xxx[1],xxx[9]);
}
Explanation:
If we initialize any array at the time of declaration the compiler will treat such array as static variable and its default value of uninitialized member is zero.

18.What will be output when you will execute following c code?
#include<stdio.h>
#define WWW -1
enum {cat,rat};
void main(){
    int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW};
    int i;
    for(i=0;i<8;i++)
         printf(" %d",Dhoni[i]);
}
Explanation:
Dhoni[0]=2
Dhoni[1]=’b’ =98  //ASCII value of character ‘b’ is 98.
Dhoni[2]=  0x3  =  3  //0x represents hexadecimal number. Decimal value of hexadecimal 3 is also 3.
Dhoni[3]=01001 = 513 //Number begins with 0 represents octal number.
Dhoni[4]  = ‘\x1d’ = 29 //’\x1d’ is hexadecimal character constant.
Dhoni[5] = ‘\111’ = 73 //’\111’ is octal character constant.
Dhoni[6] =rat = 1  //rat is enum constant
Dhoni[7] = WWW = -1  //WWW is macro constant.

19.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    long double a;
    signed char b;
    int arr[sizeof(!a+b)];
    printf("%d",sizeof(arr));
}
Explanation:
Size of data type in TURBO C 3.0 compiler is:
S.N.
Data type
Size(In byte)
1
char
1
2
int
2
3
double
8
Consider on the expression: !a + b
! Operator always return zero if a is non-zero number other wisie 1. In general we can say ! operator always returns an int type number. So
!a +b
=! (Any double type number) + Any character type number
= Any integer type number + any character type number
= Any integer type number
Note: In any expression lower type data is always automatically type casted into the higher data type. In this case char data type is automatically type casted into the int type data.
So sizeof (!a +b) = sizeof(Any int type number)  = 2
So size of array arr is 2 and its data type is int. So
sizeof(arr) = size of array * sizeof its data type = 2* 2= 4
20.What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    char array[]="Ashfaq \0 Kayani";
    char *str="Ashfaq \0 Kayani";
    printf("%s %c\n",array,array[2]);
    printf("%s %c\n",str,str[2]);
    printf("%d %d\n",sizeof(array),sizeof(str));
}
Explanation:
A character array keeps the each element of an assigned array but a character pointer always keeps the memory address of first element.
As we know %s in prints the characters of stream until it doesn’t any null character (‘\0’).  So first and second printf function will print same thing in the above program.  But size of array is total numbers of its elements i.e. 16 byte (including ending null character). While size of any type of pointer is 2 byte (near pointer).
Read More »

Most frequent questions java and c++

0 comments Posted by Unknown at 18:23

 Java and C++

A: Some of the similarities and differences are in the table:
Features Java C/C++
Pointer No Yes
Operator Overload No Yes
Typedef, Define,
Preprocessors No Yes
Structures, Unions No Yes
Enums No Yes
Functions No (only methods within classes) Yes
Goto statement No Yes
Automatic CoercionsNo(types should be converted explicitly) Yes
Global Variables No. Variable is part of a class Yes
Read More »

C

0 comments Posted by Unknown at 02:11
programming in c

Tips for c program




  
Tips For programing in c

Basically the  c program sucessive rate  depending the following things

1.Algorithm Efficiency
2.Memory
3.predicted output
4. Data Flow
Algorithm efficiency
              is used to describe properties of an algorithm relating to how much of various types of resources it consumes. Algorithmic efficiency can be thought of as analogous to engineering productivity for a repeating or continuous process, where the goal is to reduce resource consumption, including time to completion, to some acceptable, optimal level.
Memory
           Use a minium of memory space for programming storage that also increase your sucessive rate
Predicted output
                  The user are customer need the exact output that means he wants the Desired output.If achive the criteria then the sucessive rate increase
Data Flow or Reduce the code
      In your code is to lenghtly it also taken much more time to execute it.so try to avoid unnecessary statements

Example:If you are using a printf() statement in your programming each character is to take print more times compare then 1000 of times of executing a loop

                                   Top Ten tips
1.First given a meaning full name for your programming file name
2.using comments and give the detail about the code /**/
3.Declaring a variable name as meaning full
4.Avoid the looping Statement(Recursive)
5.Dont use the Goto functions
6.Using a printing statement minimum level in your program ex:printf()
7. To write a function for repeated using statements
8.Reduce the unneccessary variable
9.Allocate and use minimum of memory
10.try to reduce the programming length






Read More »

interview answers and questions

0 comments Posted by Unknown at 17:46
Using the variable a, give definitions for the following:
a) An integer
b) A pointer to an integer
c) A pointer to a pointer to an integer
d) An array of 10 integers
e) An array of 10 pointers to integers
f) A pointer to an array of 10 integers
g) A pointer to a function that takes an integer as an argument and returns an integer
h) An array of ten pointers to functions that take an integer argument and return an integer
The answers are:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers

Read More »

programing in c

1 comments Posted by Unknown at 17:39

Most common questions for embedded programmer
1. What are static variables?
2. What are volatile variables?
3. What do you mean by const keyword ?
4. What is interrupt latency?
5. How you can optimize it?
Read More »

c job interview questions in Wipro

0 comments Posted by Unknown at 23:15

What is structure in C++?
answer: The C++ programming technique allows defining user defined datatypes through structure. The syntax to declare structure is as follows:.............


What is reference variable in C++?
answer: A reference variable is just like pointer with few differences. It is declared using & operator. A reference variable must always be initialized. The reference variable.............


Read More »

c genious solved questions

0 comments Posted by Unknown at 03:42

                                                               C Solved Questions

Question: What is the output of the following code

#include <iostream>
#include <algorithm>
#include <iterator>

struct g
{
g():n(0){}
int operator()() { return n++; }
int n;
};

int main()
{
int a[10];
std::generate(a, a+10, g());
std::copy(a, a+10, std::ostream_iterator<int>(std::cout, " "));
}


Answer: 0 1 2 3 4 5 6 7 8 9
Read More »

HCL C++ Interview questions and answers

6 comments Posted by Unknown at 02:20
HCL C++ Interview questions and answers


Define structured programming.


Structured programming techniques use functions or subroutines to organize the programming code. The programming purpose is broken into smaller pieces and organized together using function. This technique provides cleaner code and simplifies maintaining the program. Each function has its own identity and isolated from other, thus change in one function doesn’t affect other.


Explain Object oriented programming.
Object oriented programming uses objects to design applications. This technique is designed toisolate data. The data and the functions that operate on the data are combined into single unit. This unit is called an object. Each object can have properties and member functions. You can call member function to access data of an object. It is based on several techniques like encapsulation, modularity, polymorphism, and inheritance.

Read More »

C,C++ Questions

0 comments Posted by Unknown at 02:04

1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived  object,. calling of that virtual method will result in which method being called? 


a. Base method 

b. Derived method..

Ans. b


2. For the following C program

#define AREA(x)(3.14*x*x)

main()
{float r1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\n Area of the circle is %f", a);
a=AREA(r2);
printf("\n Area of the circle is %f", a);
}

What is the output?

Ans. Area of the circle is 122.656250

        Area of the circle is  19.625000



3. What do the following statements indicate. Explain.
·         int(*p)[10]
·         int*f()
·         int(*pf)()
·         int*p[10]

Refer to:

-- Kernighan & Ritchie page no. 122

-- Schaum series page no. 323


Read More »

Technical interview questions -c++

1 comments Posted by Unknown at 01:58
Technical interview questions -c++
What is a class? 
What is an object?
What is the difference between an object and a class?
What is the difference between class and structure?
What is public, protected, private?
What are virtual functions?
What is friend function?
What is a scope resolution operator?
What do you mean by inheritance?
What is abstraction?
Read More »

Latest C Interview questions

0 comments Posted by Unknown at 18:14
Here are the important Latest – New – Recent Reasoning, aptitude questions and C / C++ language programs (to find errors / output) for TCS Placement Paper:
TCS Placement Test Paper Questions:
1. fn(int n,int p,int r)
{
static int a=p;
switch(n);
{
case4: a+ = a*r;
case3: a+ = a*r;
case2: a+ = a*r;
case1: a+ = a*r;
}
}
The aboue programme calculates
a.Compound interest for 1 to 4 years
b.Amount of Compound interest for 4 years
c.Simple interest for 1 year
d.Simple interest for 4 year
Read More »

TCS C Questions

0 comments Posted by Unknown at 18:05
TCS C  Questions
1. Difference between "C structure" and "C++ structure".
2. Diffrence between a "assignment operator" and a "copy constructor"
3. What is the difference between "overloading" and "overridding"?
4. Explain the need for "Virtual Destructor".
5. Can we have "Virtual Constructors"?
6. What are the different types of polymorphism?

Read More »

Depth C language job interview Questions

0 comments Posted by Unknown at 17:57
   Depth C language job interview Questions
1. Difference between "C structure" and "C++ structure".
2. Diffrence between a "assignment operator" and a "copy constructor"
3. What is the difference between "overloading" and "overridding"?
4. Explain the need for "Virtual Destructor".
5. Can we have "Virtual Constructors"?
6. What are the different types of polymorphism?
7. What are Virtual Functions? How to implement virtual functions in "C"
8. What are the different types of Storage classes?
9. What is Namespace?
10. What are the types of STL containers?.
Read More »

C important Question

0 comments Posted by Unknown at 22:04
C important Question
What are the advantages of the functions?

- Debugging is easier

- It is easier to understand the logic involved in the program

- Testing is easier

- Recursive call is possible
- Irrelevant details in the user point of view are hidden in functions
- Functions are helpful in generalizing the program
Read More »

Advanced Arrays and Pointers

0 comments Posted by Unknown at 15:51
Advanced C Arrays


In C, an array is formed by laying out all the elements contiguously in memory. The square bracket syntax can be used to refer to the elements in the array. The array as a whole is referred to by the address of the first element which is also known as the "base address" of the whole array.
{
int array[6];
int sum = 0;
sum += array[0] + array[1]; // refer to elements using []
}
0 1 2 3 4 5
array
Index
array[0] array[1] array[2] ...
The array name acts like a pointer to the first element- in this case an (int*). The programmer can refer to elements in the array with the simple [ ] syntax such as array. This scheme works by combining the base address of the whole array with the index to compute the base address of the desired element in the array. It just requires a little arithmetic. Each element takes up a fixed number of bytes which is known at compile-time. So the address of element n in the array using 0 based indexing will be at an offset of (n * element_size) bytes from the base address of the whole array. address of nth element = address_of_0th_element + (n * element_size_in_bytes)

Read More »

C variable and control Flow questions and Answers

0 comments Posted by Unknown at 14:43
C variable and control Flow questions and Answers
1. What is the difference between declaring a variable and defining a variable?
Declaration of a variable in C hints the compiler about the type and size of the variable in compile time. Similarly, declaration of a function hints about type and size of function parameters. No space is reserved in memory for any variable in case of declaration.
Example: int a;
Here variable 'a' is declared of data type 'int' Defining a variable means declaring it and also allocating space to hold it. We can say "Definition = Declaration + Space reservation".
Example: int a = 10;
Here variable "a" is described as an int to the compiler and memory is allocated to hold value 10
.
Read More »

Solved C Apptitude

0 comments Posted by Unknown at 14:34

 C Apptitude

Predict the output or error(s) for the following:
15. #include
main()
{ char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}
Answer:
77
Explanation:
p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.
Now performing (11 + 98 – 32), we get 77("M");
So we get the output 77 :: "M" (Ascii is 77).
Read More »

Infosys Solved Questions

0 comments Posted by Unknown at 14:27

Infosys Solved Questions

1.What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf (“%d",a[i]) ;
}
Ans: The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.
-------------------------------------------------------------------------------------------------
Read More »

Function question and Answers

0 comments Posted by Unknown at 14:15
Function question and Answers


1. When should I declare a function?
Functions that are used only in the current source file should be declared as static, and the function's declaration should appear in the current source file along with the definition of the function. Functions used outside of the current source file should have their declarations put in a header file, which can be included in whatever source file is going to use that function. For instance, if a function named stat_func() is used only in the source file stat.c, it should be declared as shown here:
/* stat.c */
#include <stdio.h>
static int stat_func(int, int); /* static declaration of stat_func() */
void main(void)
{
rc = stat_func(1, 2);
}
/* definition (body) of stat_func() */
static int stat_func(int arg1, int arg2)
{
return rc;
}
In this example, the function named stat_func() is never used outside of the source file stat.c. There is therefore no reason for the prototype (or declaration) of the function to be visible outside of the stat.c source file. Thus, to avoid any confusion with other functions that might have the same name, the declaration ofstat_func() should be put in the same source file as the declaration of stat_func().
Read More »

C Function questions and Answer

0 comments Posted by Unknown at 14:14

Interview Questions

1. What is a local block?
A local block is any portion of a C program that is enclosed by the left brace ({) and the right brace (}). A C function contains left and right braces, and therefore anything between the two braces is contained in a local block. An if statement or a switch statement can also contain braces, so the portion of code between these two braces would be considered a local block.

Additionally, you might want to create your own local block without the aid of a C function or keyword construct. This is perfectly legal. Variables can be declared within local blocks, but they must be declared only at the beginning of a local block. Variables declared in this manner are visible only within the local block. Duplicate variable names declared within a local block take precedence over variables with the same name declared outside the local block. Here is an example of a program that uses local blocks:

Read More »
 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top