A) What is the output of the following fragments:
1)
#include <iostream>
#include <algorithm> // for max()
using namespace std;
int myStery(int a, int b) {
if (b == 7)
return a;
else
return max(a, b);
}
int main() {
int z;
z = myStery(12, 6);
cout << "z = " << z << endl;
}
………………………………………………………………………………………………
2)
#include <iostream>
using namespace std;
int Additional(int a) {
if (a <= 1)
return 1;
return a;
}
int main() {
cout << "\nAdditional: " << Additional(5);
cout << "\nAdditional: " << Additional(-11);
cout << endl;
}
………………………………………………………………………………………………
………………………………………………………………………………………………
3) What is the output if the input number is 6?
#include <iostream>
using namespace std;
void power(int num) {
cout << "Power of " << num << " = " << num * num << endl;
}
int main() {
int num;
cout << "Enter num: ";
cin >> num;
power(num);
}
………………………………………………………………………………………………
4) Which of the following segments will call the method readData four times?
a) for (int k=1; k<4; k+=1)
readData();
b) for (int i=0; i<=4; i++)
readData();
c) for (int i=0; i<4; )
readData();
d) for (int i=0; i<4; i+=1)
readData();
………………………………………………………………………………………………
5)
#include <iostream>
using namespace std;
void nPrint(string message, int n) {
for (int i = 1; i <= n; i++)
cout << message << endl;
}
int main() {
int k = 3;
nPrint("hello", k);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
6)
#include <iostream>
using namespace std;
int squareVal(int valParameter) {
return valParameter * valParameter;
}
int main() {
int n = 5;
cout << "The square value of " << n << " = " << squareVal(n) << endl;
}
………………………………………………………………………………………………
………………………………………………………………………………………………
7)
#include <iostream>
using namespace std;
static void checkAge(int age) {
if (age < 18) {
cout << "Access denied" << endl;
}
else {
cout << "Access granted" << endl;
}
}
int main() {
checkAge(20);
}
………………………………………………………………………………………………
8)
#include <iostream>
using namespace std;
static int Ave(int i, int j) {
int r = (i + j) / 2;
return r;
}
int main() {
int i = 4, j = 2;
int result = Ave(i, j);
cout << result;
}
………………………………………………………………………………………………
9)
Which of the following method headers does the following method call match?
action("This is an example", 15, 25.5)
a) action()
b) action(int x, double y, string z)
c) action(double y, string x , int z)
d) action(string x, int y, double z)
………………………………………………………………………………………………
B) Find the errors and correct them:
1)
#include <iostream>
using namespace std;
static void G() {
cout << "Inside method G" << endl;
static void H() {
cout << "Inside method H" << endl;
}
}
int main() {
H();
G();
}
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
#include <iostream>
using namespace std;
static int Sum(int x, int y) {
int result;
result = x + y;
}
int main() {
Sum(5, 7);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
#include <iostream>
using namespace std;
static void F(float a);
{
float a;
cout << "a= " << a << endl;
}
int main() {
F(10.5f);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
4)
#include <iostream>
using namespace std;
static void Product() {
int a = 6, b = 5, c = 4, result;
result = a * b * c;
cout <<"Result is " << result << endl;
return result;
}
int main() {
Product(13, 14);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
5)
#include <iostream>
using namespace std;
static method(int i) {
i = i * 2;
}
int main() {
double i = 10.3;
method(i);
cout << i << endl;
}
………………………………………………………………………………………………
………………………………………………………………………………………………
6)
#include <iostream>
using namespace std;
static void Times(int x, int y) {
cout << x * y << endl;
}
int main() {
string i = "7";
string j = "3.5";
Times(i, j);
cout << "Done" << endl;
}
………………………………………………………………………………………………
7)
#include <iostream>
#include <string>
using namespace std;
static void myMethod(string stdName, int stdNumber, double degree) {
cout << "Name is " << stdName << endl;
cout << "Number: " << stdName << endl;
cout << "Degree = " << degree << endl;
}
int main() {
string name = "Joury";
int number = 4225689;
double d = 99.9;
myMethod(name, number);
myMethod(number, name, d);
myMethod("Mona", 435897, "A+");
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
C) Write a Java program:
1. Write a program that reads a number from the user and send it
to a method named Iseven which returns true the number is
even otherwise it returns false. Call the method properly.
2. Write a program to create a static method named add which
accepts two numbers entered from a user and returns sum of
the numbers. Call the method properly.
3. Write a program to create a function to swap the values of
two integer numbers. Call the method properly.
4. Write a complete program that reads the length and width of a
rectangle and send them to a method named getArea to compute and
return the area of the rectangle. Call the method properly.
Area = Length * Width
5. Write a program of tax calculation. Accept the name and money as
input from the user and send money to the method named
CalculatTax to calculate the tax using following pattern. Then print
out the name of the user and his total tax.
Money Percentage Total Tax
Less than 10,000 5% ?
10,000 to 100,000 8% ?
More than 100,000 8.5% ?