Languages-C & C Plus Plus [TCS Placement]: Sample Questions 49 - 50 of 119
Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.
Question 49
Explanation
- Yes, it is possible to create a customized header file which include function prototypes used in the program
- #include directive followed by the name of your header file will be used in the source.
- How to create own header files in c
- Open notepad and write the function used in the program. An example is shown below.
int add(int a,int b)
{
return(a+b);
}
- Now save the notepad file with . h extension.
- In the “c” source file include the header file
#include<stdio.h>
#include<conio.h>
#include<add.h> //header file created
void main()
{
int a,b,s;
clrscr();
printf(“Enter the value of a and b:”);
scanf(“%d%d”,&a,&b);
s=add(a,b);
printf(“Add=%d”,s);
getch();
}
Question 50
Question MCQ▾
Which of the following is correct about class and structure?
Choices
Choice (4) | |
---|---|
a. | Pointer to structure or classes cannot be declared. |
b. | Class data members are private by default while that of structure are public by default. |
c. | Class data members are public by default while that of structure are private. |
d. | Class can have member functions while structure cannot. |
Answer
b.Explanation
- Structure members are public by default and hence can be accessed anywhere in the program through structure variable.
- Class members, on other hand are private to provide security and code encapsulation.
- C ++ , structures can also contain member functions, just like in Class- but semantics are same as that of C structures.
- Here՚s an example:
#include<stdio.h>
struct mystruct
{
int a ;
void display(void)
{
printf("Hello world...[%d]",a);
}
} s;
int main()
{
s.a=10;
s.display();
return 0;
}
- Output:
Hello world … [10]