CUSTOMER_CODE SMUDE DIVISION_CODE SMUDE

advertisement
CUSTOMER_CODE
SMUDE
DIVISION_CODE
SMUDE
EVENT_CODE
OCTOBER15
ASSESSMENT_CODE MCA2030_OCTOBER15
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
11811
QUESTION_TEXT
What is the basic difference between other array and string? Explain any
three C++ string function with syntax and example.
SCHEME OF
EVALUATION
One basic difference between other arrays and strings is that the
compiler stores an extra null character (‘\0’) for every string to make the
end of the string.
C++ has several string related functions that are used to manipulate
strings. They are
1. Strrev function: this function arranges the characters in the string
variable in reverse order except for the null character. The syntax of
strrev function is strrev(string variable). It returns the reversed string.
For example in the following program statement, the output will be
olleh. char str[10]=”hello”;
cout<<strrev(str);
2. Strlen function: Strlen function is used to find the length of the string.
The syntax of the strlen function is strlen (string variable). The function
returns the length of the string or the number of characters in the string
which is a integer. Example: char str[10]=”hell”;len=strlen(str);
3. Strcpy function: this function copies the contents of one string to
another. The syntax of strcpy function is strcpy(destination string, source
string). The function returns the destination string value after copying.
For example, in the following program statement, the contents of the
string str will be copied to string namechar str[10]=”ravi”;char
name[10];strcpy(name, str);
4. Strcmp function: this function is used to compare two strings. The
syntax of strcmp is strcmp(string1,string2). Every character of string1
will be compared with corresponding character of string2.the ASCII
values of the character will be used to decide the return value. The
function return an integer and value returned will differ based on the
condition a shown below
if string1 <string 2, value returned will be <0
if string1 == string 2, value returned will be 0
if string 1> string 2, value returned will be greater than zero. Char
str[10]=”abc”; char [10]=”ABC”;
example : strcmp(str,str1) returns greater than zero(32)
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
11812
QUESTION_TEXT
Without example Write a note on:
(i)Passing by value
(ii)Passing by reference
(iii)Automatic variable
(iv)External Variable
(v)Static variable
SCHEME OF
EVALUATION
i. Passing by value: In this, a copy of the variable (main program) is
created during function call with the name specified in the function and
initialized with the value in the original variable. All the operations in
the function are then performed on the function variable. The values in
the variables declared in the main program remain unchanged by the
function operations.
ii. Passing by reference: In this, no copy of the variable is crated.
However, the variables in the main program are referred to by different
name in the function. Since no copy is created, when the values in the
function variables are modified, the values in the variables in the main
program are also modified. Passing by reference provides an easy
mechanism for modifying the variables by functions and also enables to
return multiple variables. To pass arguments by reference, all the
variables names in the argument list should be prefixed with & during
function declaration and definition.
iii. Automatic variable: it is default variable in C++. All the variables
we have created and used in programs are automatic variables. These
variables can also be declared by prefixing the keyword auto. One of
important characteristics of these variables are that are created only when
the function or program block in which they are declared are called.
Once the execution of function or module execution is over, the
variables are destroyed and the values are lost.
iv. External variable: these are variables external to any function. Thus
they can be accessed by multiple functions. This is the easiest way to
share data among various functions. This feature is just to support
backward compatibility with C. external variables are also know as
global variables and are automatically initialized to zero. These variables
are declared before main and declaration statement does not belong to
any function. .
v. Static Variable: these are a mix of external and automatic variables.
They are the scope of the function in which they are defined like
automatic variables. But they can retain their values even after the
function execution is over and are automatically initialized to zero like
external variables.
QUESTION_TYP
DESCRIPTIVE_QUESTION
E
QUESTION_ID
72994
QUESTION_TEX Explain break, continue,goto and exit statements in C++ with example
T
program
SCHEME OF
EVALUATION
1. Break statement:
(3 marks)
1. Transfers control to the next statement after the loop.
2. Remaining statements and iterations in loop are skipped
3. It is used if you would like to exit the loop when a certain condition is
met.
4. Ex: #include< iostream.h >
Void main()
{
Int number;
Int prime=0;
Cout< < ”Enter a number”;
Cin > >number;
For(int i=2;i< =number/2;i++)
If(number%i)==0)
{
Prime=1;
Break;
}
}
If(prime==0)
Cout< < ”the number is prime number”;
Else
Cout< < “the number is not a prime number”;
}
2.continue statement
(3 mar
ks)
1.Statement is used to take control to the next iteration of the loop.
2.Used if the control has to be transferred to the next iteration of the loop
based on a condition skipping all the other statements in the loop.
3.Example:
#include< iostream.h >
#include< conio.h >
Void main()
{
Int x,y;
Char choice=’y’;
Do
{
Clrscr();
Cout< < please enter the dividend”;
Cin > >x;
Cout< < please enter the divisor”;
Cin > >y;
If(y==0)
{
Cout< < ”error! Division by zeo”;
Continue;
}
Cout< < ”the quotienet is”< < (x/y);
Cout< < ”the remainder is”(x%y);
Cout< < ”do u wish to continue”;
Cin > >choice;
}while(choice==’y’);
}
3 Exit:
marks)
1.Function in a program takes the control out of the program.
2.Exit is an inbuilt library function.
3.Process.h should be included.
4.Syntax: exit(integer)
5.Integer should be returned to the o.s.
6.0-no error,successful termination.
7.Non-zero error.
4.go to:
marks)
1. Statement should be avoided.
2. It take the control to a particular statement.
3. The statement should have a label.
4. Syntax: goto statement labelname.
Labelname:statement.
(2
(2
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
72995
QUESTION_TEXT
Explain the three steps in Executing a C++ program. Explain in brief four
types of errors encountered during compilation.
SCHEME OF
EVALUATION
There are three steps in executing a C++ program:
Compiling: The c++ programs have to be typed in a compiler. The turbo
c++ compiler comes with an editor to type and edit c++ program. After
typing the program the file is saved with an extension .cpp. This is
known as source code. The source code has to be converted to an
object code which is understandable by the machine. This process is
known as compiling the program. You can compile your program by
selecting compile from compile menu or press Alt+f9. After compiling a
file with the same name as source code file but with extension .obj. is
created.
Linking: Second step is linking the program which creates an executable
file .exe (filename same as source code) after linking the object code
and the library files (cs.lib) required for the program. In a simple
program, linking process may involve one object file and one library file.
However in a project, there may be several smaller programs. The
object codes of these programs and the library files are linked to create
a single executable file. Third and the last step is running the executable
file where the statements in the program will be executed one by one.
Running the program: .When program is executed, the compiler
displays the output of the program and comes back to the program
editor. To view the output and wait for user to press any key to return
to the editor, type getch() as the last statement in the program. Getch()
is an inbuilt predefined library function which inputs a character from
the user through standard input. However you should include another
header file named conio.h to use this function. Conio.h contains the
necessary declarations for using this function. The include statement
will be similar to iostream.h.
(2 Marks each)
During compilation, if there are any errors that will be listing by the
compiler. The errors may be any one of the following:
1. Syntax error
This error occurs due to mistake in writing the syntax of a c++
statement or wrong use of reserved words, improper variable names,
using variables without declaration etc.
2. Logical error
This error occurs due to the flaw in the logic. This will not be
identified by the compiler. However it can be traced using the debug
tool in the editor.
3. Linker error
This error occur when the files during linking are missing or mispelt
4. Runtime error
This error occurs if the programs encounters division by zero,
accessing a null pointer etc during execution of the program
(1 Marks each)
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
72996
QUESTION_TEXT
Explain in brief the functions - tellp(),seekp(),ignore(), peek() and
putback()
SCHEME OF
EVALUATION
tellp() – The same as tellg() but used when we write in a file. To
summarize: when we read a file, and we want to get the current position
of the inside-pointer, we should use tellg(). When we write in a file, and
we want to get the current position of the inside-pointer, we should use
tellp().
seekp() – Remember seekg()? We used it when reading a file, and we
wanted to go to a specified position. seekp() is the same, but it is used
when you write in a file. For example, if we read a file, and we want to
get 3 characters back from the current position, we should call
FileHandle.seekg(-3). But if we write in a file, and for example, we want
to overwrite the last 5 characters, we have to go back 5 characters. Then,
we should use FileHandle.seekp(-5).
ignore() – Used when reading a file. If you want to ignore certain
amount of characters, just use this function. In fact, you can use seekg()
instead, but the ignore() function has one advantage that you can
specify a delimiter rule, where the ignore() function will stop.
peek() – This function will return the next character from an input file
stream, but it won’t move the inside-pointer. get() for example, returns
the next character in the stream, and after that, it moves the insidepointer, so that the next time you call the get() function, it will return the
next character, but not the same one. Well, using peek() will return a
character, but it won’t move the cursor. So, if you call the peek()
function, two times in succession, it will return the same character.
putback() – This function will return the last read character, and will
move the inside-pointer, one with –1 char. In other words, if you use
get() to read a char, then use putback(), it will show you the same
character, but it will set the inside-pointer with –1 char, so the next
time you call get() again, it will again show you the same character.
(2 marks each)
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
125607
QUESTION_TEXT
Define a structure named product with elements productcode,
description, unitprice and qtyinhand. Write a C++ program that
implements the structure and enables to store at least 100 product data.
Ans: #include<iostream.h>
SCHEME OF
EVALUATION
structure product //3 marks
{
int productcode;
char description;
float unit price;
int qtyinhand;
} p[100];
void main() //2 marks
{
int i;
for(i=0;i<100;i++)
{
cout<<”enter the product code”; //2 mark
cin>>p[i].productcode;
cout<<”enter the product description”; // 1 mark
cin>>p[i].description;
cout<<”enter the unit price”; // 1 mark
cin>>p[i].unitprice;
cout<<”enter qty”; //1 mark
cin>>p[i].qtyinhand;
}
}
Download