Friday, May 20, 2016

Basic puzzle questions in C/C++ :: MUST KNOWN concepts to every programmers :: SET-3

Q1.
string str;
int ii; double dd;
cin>>ii;
cout<<ii+i<<endl;
cin>>dd;
cout<<dd+d<<endl;
getline(cin,str);
cout<<str<<endl;


Q2.
double d=4;
cout<<d<<endl;

Q3.
double d=4;
cout<<setprecision(1)<<fixed<<d<<endl;

Q4.
Which one is suitable syntax for function template?
a. template< class T> return_type Function_Name(parameters)
b. template< typename T> return_type Function_Name(parameters)
c. both and b
d. None of these

Q5.
In CPP, the size of the character array should be one larger than the number of characters in the string.
a. True
b. False

Q6.
A class can contain objects of other classes and this phenomenon is called__________
a. Relationship
b. Object Association
c. Containership
d. None of these

-----------------------------------------------------------------------------------------------------------------
Answers:::
1)
It will take input in str. We need to use cin.ignore();
cin.ignore();  ----cin.ignore() is used to ignore previous cin values. string takes \n(new line) as input.
Correct program:-
string str;
int ii; double dd;
cin>>ii;
cout<<ii+i<<endl;
cin>>dd;
cout<<dd+d<<endl;
cin.ignore();
getline(cin,str);
cout<<str<<endl;

2)
O/P-  4

3)
O/P-  4.0

4)
Answer:: c (both a and b)

5)
Answer:: a ::True
void main()
{ char name[5] = “INDIA”; //This is invalid and cause error
  char x_name[6]=”INDIA” // This is valid, one extra byte required for \0 character
}

6)
Answer:: Containership


Structure in C vs Structure in C++ :: Can we define function in structure

Structure in C vs Structure in C++ :: Can we define function structure

There are few major difference between structure in C and C++. While I was studying I always remains confused if structure can have function or not, if yes then whether in C or in C++.
Because I have studied that structure can not have function as member function. But in internet in many places I have seen function inside structure by searching many websites I found yes structure can also have member function but only in C++ not in C.
I have written a short program to understand it.

The important difference between class and structure is that by default in class member remains in Private while in structure it remain in public.

In C we can not define function inside structure but we can use pointer to function here.

In C++
We can do all things as we do in Class in C++ except the members of structure always remain public.
We can define function,constructor,destuctor in Structure also in C++.

See the below code of structure written in C++ for better understanding. It explains how we can define function,constructor and de-structure in structure in C++.

#include<iostream>
#include<cstring>
using namespace std;
struct student
{
    int roll_no;
    char *name;
    student()
    {
        roll_no=0;
        name = new char[100];
        }
    student(int rn,const char* name1)
    {
        roll_no=rn;
        name = new char[strlen(name1)+1];
        strcpy(name,name1);
    }
    void getData()
    {cout<<"Enter Roll number::";
        cin>>roll_no;
        cout<<"\nEnter name::";
        cin.ignore();
        cin.getline(name,100);
    }
    void display()
    {
        cout<<"\nRoll Number::"<<roll_no;
        cout<<"\nName::"<<name;
    }
    ~student()
    {
        cout<<"\nDestructor called for"<<this;
    }
};
int main( )
{
    student s1;
    s1.getData();
    s1.display();
    student s2;
    s2=s1;
    s2.display();
    student s3(226,"Mukesh Kumar");
    s3.display();
    return 1;
}












Please share your views and knowledge. Please comment below for any more information and also if you find any thing wrong in this post.

Friday, May 13, 2016

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers :: SET-2

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers

Q1) What will be output of the program below?
void print (int n)
{
if (n>0)
{
printf(“hello”);
print(n-1);
}
printf(“world”);
}


Q2) Which statement will cause problem?
int main()
{
Statement 1-> const char *p1=”hello world”;
Statement 2-> char *const p2=”hello world”;
Statement 3-> p1=”welcome”;
Statement 4-> p2=”good bye”;
Statement 5-> *(p1+3) = ‘A’;
Statement 6­-> *(p2+3) = ‘A’;
return 0;
}

Q3)  What will be output ?
int function()
{   static int a=0;
    a=a+1;
    return a;
}
main()
{   function();
    function();
    printf("%d", function());
}

Q4)  What will be output ?
int main(){
    int x=5;
    printf("%d %d %d\n",x,x<<2,x>>2);
}

Q5)
int main(){
 int i=5;
 printf("%f",i);
    return 0;
}

Q6) What is the Error?
main()
{ int x=3;
return;
}

Q7) What will be output ?
struct ABC
{
    int var1,var2;
    void (*func);
};
int max (int x, int y)
{
     return (x>y)?x:y;
}
int main () {
   struct ABC abc;
   abc.var1=5; abc.var2=8;
   abc.func = max(abc.var1,abc.var2);
   printf("%d",abc.func);
   return 0;
}


---------------------------------------------------------------------------------------------------------
ANSWERS::
1)
It will print, N times “hello” and then by N+1 times “world”.
First it will call print function recursively N times and will print world after it comes out of the if block. “world” is printed N+1 times since when n ==0, it will not go inside if block and will print world and return.

2)
Answer:: Statement 4 and 6

3)
Answer::3

4)
Answer:: 5 20 1

5)
Answer::0.0033 or any decimal value

6)
Answer:: Return type of main is not defined even-though return statement is used

7)
Answer::8  :: This is the way in which we can define function for structure in C. We define pointer of function pointer instead of function itself in structure.

Thursday, May 12, 2016

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers
Please try the below puzzles, which are very basic and also very useful in clearing the concept. Find the answer of all the puzzles in last page.

What will be the output of C puzzles below :-
Q 1>
#include<stdio.h>
int main()
{
  char *p=(char *)malloc(sizeof(char)*4); p="abc";
   char q[]="abc123";
     if(p[0]=q[0])
   {
       printf("%c, %c\n",*p,*q);
   }
}

Q 2>
#include<stdio.h>
int main()
{
  char *p=(char *)malloc(sizeof(char)*4); p="abc";
   char q[]="abc123";
     if(q[0]=p[0])
   {
       printf("%c, %c\n",*p,*q);
   }
}


Q 3>
main(){
    int num[] = {1,4,8,12,16};
    int *p,*q;
    int i;
    p =  num;
    q = num+2;
    i = *p++;
    printf("%d, %d, %d\n",i, *p, *q);
}

Q 4>
main(){
char *a[4]={"jaya","mahesh","chandra","swapant"};
int i = sizeof(a)/sizeof(char *);
printf("i = %d\n", i);
}

Q 5>
void fun(int *a, int *b)
{
 int *t;
 t=a;
 a=b;
 b=t;
printf(%d, %d\n",*a, *b);
}
main()
{
     int a=2;
     int b=3;
     fun(&a,&b);
     printf(%d, %d\n",a, b);
}

Q 6>
main(){
    int i =0;
    i = 2+3, 4>3, 3;
    printf("%d", i);
}

Q 7>
main(){
    printf("%u", -1);
}

Q 8>
struct ABC{
    float a:10;
    int b:6;
    char c;
} structure;
int main(){
    int i = sizeof(struct ABC);
    printf("%d", i);
}

Q 9>
#include<stdio.h>
int main() {
    char x[] = "abc";
    char *y = x;
    *y ='1';
    printf("%s",y);
}

Q 10>
main(){
    int i =0;
    i = 4>3, 2+3, 3;
    printf("%d", i);
}

Q 11>
#include<stdio.h>
void fun(int str,...);
int main()
{
    fun(6,"Hello","World","5","6","7","8");
    return 0;
}
void fun(int str,...)
{
    int i;
    int *n=&str;
    for(i=1;i<=str;i++)
    printf("%s ",*(n+i));
    return 0;

}

ANSWERS::
1> Segmentation Fault
   Reason-> We can't assign value to pointer valriable like this.
Different ways to assign value to pointer valriable. 
char *p="Hello World";
char *p=new char[11];  p="Hello World";
Wrong ways which lead to segmentation fault::
p[0]='a'; if(p[0]=='a'){};  *p='a'; if(*p=='a'){};
2> a, a

3> 1, 4, 8

4> 4

5> 3, 2
   2, 3
6> 5
   Explanation:: It stores only the first value.

7> 4294967295   :: As the size of unsigned int is from 0 to 4,294,967,295. -1 will print the back side of zero i.e. 4294967295.

8> Compilation Error:: We cannot use but filed for Float.

9> 1bc

10> 1  ::4>3 return 1 as it is true.

11> Hello World 5 6 7 8


Please comment if you find any thing wrong also please post the questions which you feel need to be shared.