Engr 123 Name____________________ Hour Exam 1 February 4, 2015

advertisement
Engr 123
Hour Exam 1
Name____________________
February 4, 2015
1. How many line of print do each of the following produce? The variables i, j, and k are
integers.
for(i=0;i<=10;i++)
j = -1;
{j = 1;
while(j < 7)
while(j < 1000)
{Console.WriteLine(j);
{Console.WriteLine(i, j);
for(k=1;k<5;k++)
j = j + 2;
{Console.WriteLine(j*k);
}
}
}
j++;
}
Lines of Print ______________
Lines of Print________________
2. Show what is printed as a result of each of the following: (all variables have been declared to
be of type int.
A)a = -7;
B)a = 10;
C)a = 19;
b = 3;
c = a/b + 1;
Console.Write(c);
Printed Result______
b = 3;
c = 2*a/b;
Console.WriteLine(c);
Printed Result______
b = 5;
Console.WriteLine(a % b);
Printed Result_______
3. If i = 3, j = 12, and k = 4, determine whether each of the following is TRUE or
FALSE.
A) ((i == 4)&&(j != 10)) ________
B) (!(!(j > i)||(k < 4))) _______
4. If I, j, and k are integers and i = 9 and j = 22 what does the following print?
k = ++i - ++j/2 + 4;
Console.WriteLine(k);
________________________
5. Write an equation in C# for the following algebraic equation:
b2
x=a×
+ 9.23 _______________________
c+d
6. Shown below is a loop using a while structure. Rewrite the loop to do the same task
but use a for structure.
int i = 1;
while(i<7)
{Console.WriteLine(i);
i += 2;
}
7. Given below is a main program and two private methods. Fill in the memory map to
show the value of the variables that are stored as the program runs and show what the
program prints.
static void Main(string[] args)
{int i, j;
i = 5;
j = 22;
Console.Write(i);
Console.WriteLine(j);
First(i, j);
Console.Write(i);
Console.WriteLine(j);
}
private static void First(int j, int i)
{int k;
k = 12;
Console.Write(i);
Console.WriteLine(j);
k = i + j/2;
Console.WriteLine(k);
}
First
Main
Data
Printed Results
8. The statements below prompt the user to enter two integers called i and j. Write an if block
to print the value of i and j only if the value of i is greater than 10 and i is greater than j. If this
is not the case your if block should print only the value of i.
int i, j;
Console.Write( "Enter an integer...");
i = Convert.ToInt32(Console.ReadLine());
Console.Write( "Enter an integer...");
j = Convert.ToInt32(Console.ReadLine());
// Put your if block here
9. Write a program to evaluate the algebraic equation given by y = 12.7x3 – 52/7 for
values of x starting at x = –1 in increments of 0.1 until y is greater than or equal to
10,000. Print only the first value of y and the corresponding value of x for which y >
10,000.
10. Write a program to prompt the user for input and print the sum of the last two integers which
were entered. Stop when the user enters a 0. A sample program output might look like this:
Enter and int… 5
Enter and int… 7
Sum is 12
Enter and int… 3
Sum is 3
Enter an int… 9
Sum is 12
Enter an int…0
K
11. Consider the series given by
∑1 / x
n
= 1 + 1 / x + 1 / x 2 +  + 1 / x K . Write a program to
n =0
prompt the user to input a value for K and a value for x. Calculate and print the value of the
series.
12.Given the static private method that is part of a main program (not in a separate class).
static private int FirstMethod(int x, int y)
{return x*y + 2;
}
A) Write an example of a typical calling statement that goes in the main program.
___________________________________________________
B) If the return statement in the method was changed to
return x*y + 2.0;
it would generate an error. Explain why?
Download