Explore BrainMass
Share

C++ programming and conceptual questions

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

12. Write a program that reads in an integer from the keyboard and displays its value doubled, using two functions that you write. The reading function should be int and the displaying function should be void.

13. How does the use of inline speed up the execution of a program? What is the disadvantage of using inline?

14. What is the output produced by the following program? Explain.
#include <iostream>
using namespace std;
void testfunc(int x, int y = 10) {
cout << x + y;
}
int main () {
testfunc(3);
return 0;
}

15. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
void f(int i, int &j) {
i = 5;
j = j + i;
cout << "f: i = " << i << endl;
cout << "f: j = " << j << endl;
}
int main () {
int i = 15;
int j = 30;
f(i, j);
cout << " main: i = " << i << endl;
cout << " main: j = " << j << endl;
}

16. Write a program that reads in two integers from the keyboard and displays their sum. The reading function should read in both integers at the same time. Both the reading and displaying functions should be separate void functions.

17. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
float someNum = 3.0;
int main () {
int someNum = 2;
cout << someNum << endl;
cout << ::someNum << endl;
return 0;
}

18. When two or more functions have the same name, how does the compiler determine which one to use for a particular function call?

19. Write a void function called swap that takes two integer parameters and swaps their contents. Write another swap function that takes two double arguments. Write a short main function that calls both swap functions and demonstrates that the contents have been swapped. Cout statements should only appear in function main.

20. Why do we have both debug and release builds?

21. What is the difference between step over, and step into?

22. Find and describe the errors in the following program. Use the debugger.
//this program computes how much money will
//accumulate after so many years of investing
#include <iostream>
using namespace std;
double Balance = 0.0;
double Interest;
double YearlyCont;
int NumYears;
//this function computes one year of investment
double newBalance(double balance);
int main () {
cout << "How much money will you deposit"
<< "each year?" << endl;
cin >> YearlyCont;
cout << "What interest rate will you get"
<<"(enter 5% as .05)?" << endl;
cin >> Interest;
cout << "How many years will you invest?"
<< endl;
cin >> NumYears;
for(int i = 1; i <= NumYears; i++);
Balance = newBalance(Balance);
cout << "Money at end of investment: "
<< Balance << endl;
return 0;
}
double newBalance(double balance) {
//add in this year's deposit
balance = balance + YearlyCont;
//add in this year's interest
balance = (Interest + 1.0) * Balance;
return balance;
}

© BrainMass Inc. brainmass.com October 25, 2018, 3:11 am ad1c9bdddf
https://brainmass.com/computer-science/genetic-approximation/programming-conceptual-questions-330388

Attachments

Solution Preview

Please find the solution in the attached file.

12. Write a program that reads in an integer from the keyboard and displays its value doubled, using two functions that you write. The reading function should be int and the displaying function should be void.
SOLUTION 12
#include<iostream.h>
#include<conio.h>
int read()
{
int n;
cout<<"nEnter an integer";
cin>>n;
return n;
}

void display(int value)
{
cout<<"nnNumber entered by user is "<<value;
cout<<"nnAfter doubling the value number becomes "<<2*value;
}

int main()
{
int number=read();
display(number);
getch();
}

13. How does the use of inline speed up the execution of a program? What is the disadvantage of using inline?
SOLUTION 13
The use of inline speeds up the execution of a program. Instead of calling the function every time it is invoked, the compiler replaces the function call with a copy of the function body. If it's a small function which gets called a lot, this can sometimes speed things up.
Disadvantage of using inline
Since the compiler will copy the entire function body every time the function is called, if it is a large function (more than three or four lines), inlining can increase the size of your executable program significantly.

14. What is the output produced by the following program? Explain.
#include <iostream>
using namespace std;
void testfunc(int x, int y = 10) {
cout << x + y;
}
int main () {
testfunc(3);
return 0;
}
SOLUTION 14
OUTPUT is 13
Function testfunc(int x, int y = 10) has two parameters "x" and "y" out of which "y" is a default parameter whose value is 10 .
A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used.
In main as testfunc is called with only one argument value '3', thus that value is assigned to "x" and "y" takes the default value i.e. 10.
Therefore, x + y = 3 + 10 = 13

15. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
void f(int i, int &j) {
i = 5;
j = j + i;
cout << "f: i = " << i << endl;
cout << "f: j = " << j << endl;
}
int main () {
int i = 15;
int j = 30;
f(i, j);
cout << " main: i = " << i << endl;
cout << " main: j = " << j << endl;
}

SOLUTION 15
f: i = 5
f: j = 35
main: i = 15
main: j = 35
Here in the above given program, from the main function, i is passed "by-value" and j is passed "by-reference" to the function f(int i,int &j).
In the main function, the initial values of i and j are 15 and 30 respectively at the time of calling function f(i,j).Now in the function f(i,j), i is initialized to 5 and j = j+ i which is equal to 30+5 = 35. Therefore, the values printed in the ...

Solution Summary

C++ programming and conceptual questions are examined.

$2.19
See Also This Related BrainMass Solution

Programming Languages Multiple Choice Questions

1. What does the term aliasing mean?
a. Having two names for the same memory cell
b. Having two referencing methods for the same memory cell
c. Both a and b.
d. Neither a nor b.

2. The most rigorous means of describing the meaning of a program is
called _________ semantics.
a. dynamic
b. denotational
c. static
d. axiomatic

3. Lexical analysis is separated from syntax analysis because:
a. lexical analysis is less complex.
b. optimizing the lexical analyzer provides more benefits than
optimizing the syntax analyzer.
c. lexical analysis has to perform input/output operations and,
therefore, is platform dependent.
d. all of the above.

4. The output of _____ compilers must be linked to operating system
programs, especially __________ routines.
a. all; input/output
b. all; device drivers
c. some; input/output
d. some; device drivers

5. The extreme opposite of a compiler is known as a(n):
a. interpreter.
b. translator.
c. linker.
d. hybrid.

6. The following list contains steps that may/may not be part of the
process of compilation. Place the steps in the correct order that are
part of the compilation process, omitting those that are not part of
the compilation process.
i. lexical analysis
ii. linking
iii. formatting
iv. semantic analysis
v. optimization
vi. code generation
vii. syntax analysis
viii. loading
a. i, ii, iii, iv, v
b. iv, i, vii, v, ii
c. i, vii, vi, iv, v, ii
d. vii, i, iv, v, vi, iii
e. i, vii, iv, v, vi

7. The data structure built by a compiler that represents the
hierarchical syntactic structure of a given program is called a(n)
__________ tree.
a. parse
b. trace
c. syntax
d. module

8. How is Scheme opposite to Common Lisp?
a. Scheme is large and complex; Common Lisp is small.
b. Common Lisp is not an ANSI standard; Scheme is.
c. Scheme is exclusively statically scoped; Common Lisp supports
dynamic scoping.
d. Common Lisp is interpreted; Scheme is compiled.

9. The primary progenitor of COBOL was the programming language
known as:
a. Pseudocode.
b. Short Code.
c. Speedcode.
d. Flow-matic.

10. The C++ programming language is very popular because it is:
a. backward compatible with C.
b. object-oriented.
c. widely available.
d. all of the above.

11. Plankalkül included the following features except:
a. (recursive) record structures.
b. ones-complement notation floating point type.
c. iterative statement.
d. arrays.

12. For a large production application, a __________ language is most
likely to be used:
a. compiled
b. interpreted
c. hybrid
d. script

13. Java and Perl are examples of __________ languages.
a. compiled
b. interpreted
c. hybrid
d. script

14. What innovation introduced in ALGOL68 is often credited to Pascal?
a. user-defined data types
b. block statement
c. select statement
d. bit-string

15. The syntax analysis part of a compiler is known as a:
a. language generator.
b. language recognizer.
c. context free grammar.
d. assembler.

16. If the left-hand side (LHS) appears in the right-hand side (RHS) of a
rule, it is a(n):
a. error.
b. iteration.
c. meta-statement.
d. recursion.

17. A hierarchical structure that describes the syntactic structure of
sentences in a language is called a(n):
a. descendant chart.
b. family tree.
c. parse tree.
d. ancestor list.
e. none of the above.

18. An alternate term for lexical analyzer is:
a. pattern matcher.
b. parser.
c. pushdown automaton.
d. all of the above.

19. A concept of ________ means that a small number of primitive
constructs can be combined in a small number of ways to build the
control and data structures of the language.
a. simplicity
b. parsimony
c. relativity
d. orthogonality

20. The task of a bottom-up parser is to find the handle of any given
right sentential form that can be generated by its associated
grammar.
a. True
b. False

21. A logical rule that is assumed to be true is termed a(n):
a. axiom.
b. theorem.
c. corollary.
d. inference.

22. Using EBNF, the C selection statement is depicted as:
<selection> &#1048774; if (<expression>) <statement> [else <statement>];
Without brackets, how many BNF rules would be required to depict
the C selection statement?
a. 1
b. 2
c. 3
d. More than 3

23. With a von Neumann architecture, assignment is based on:
a. pipelining operations.
b. piping operations.
c. iterative operations.
d. functional operations.

24. The most common programming languages, dating back to the
1940s are called:
a. functional.
b. object-oriented.
c. rule-based.
d. imperative.

25. Consider the rule: A &#1048774; A + B. Using a LL parser, this rule is
impossible to handle.
a. True
b. False

View Full Posting Details