SampleExam03(Ch5).

advertisement
Name:_______________________
Chapter 5 on Methods
CSCI 1301 Introduction to Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
(50 minutes)
Part I: Multiple Choice Questions: (1 pts each)
1.
A variable that is declared inside a method is called
________ variable.
a.
a static
b.
an instance
c.
a local
d.
a global
e.
a class
2.
Each Java class must contain a main method.
a.
true
b.
false
3.
What is (int)Math.random()?
a.
0
b.
1
c.
both 0 and 1 are possible
d.
None of the above
4. Analyze the following code:
class Test {
public static void main(String[] args) {
System.out.println(xMethod((double)5));
}
public static int xMethod(int n) {
System.out.println("int");
return n;
}
public static long xMethod(long n) {
System.out.println("long");
return n;
}
}
a.
b.
c.
d.
e.
5.
The program
The program
The program
in a and b.
The program
None of the
displays int followed by 5.
displays long followed by 5.
runs fine but displays things other than given
does not compile.
above.
You may have a return statement in a void method.
a. true
b. false
1
Note: Questions 6–8 are based on the following method:
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
6.
What is the printout of the call nPrint("a", 4)?
a.
aaaaa
b.
aaaa
c.
aaa
d.
invalid call
7.
What is k after invoking nPrint(“A message”, k+1)?
int k = 2;
nPrint("A message", k+1);
a.
b.
c.
d.
e.
8.
0
1
2
3
None of the above.
Analyze the following code.
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
int k = 3;
nPrint("A message", k);
}
System.out.println("k is " + k);
}
a.
b.
c.
d.
e.
9.
The code has a syntax error because k is not defined
in System.out.println("k is " + k).
The code prints k is 0.
The code prints k is 1.
The code prints k is 2.
The code prints k is 3.
Analyze the following code.
public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);
System.out.println("n is " + n);
}
public static void xMethod(int n) {
2
n++;
}
}
a.
b.
c.
d.
e.
The code
The code
The code
The code
return a
The code
declared
prints n is 1.
prints n is 2.
prints n is 3.
has a syntax error because xMethod does not
value.
has a syntax error because xMethod is not
static.
10.
Which
a.
b.
c.
d.
of the following is
Using methods makes
Using methods makes
Using methods makes
Using methods hides
clients.
not an advantage of using methods.
program run faster.
reusing code easier.
programs easier to read.
detailed implementation from the
11.
Which of the following is a possible output for 50 *
Math.random()?
a.
0
b.
50
c.
100
d.
500
e.
a and b.
12.
Which
a.
b.
c.
d.
e.
of the following method results in 8.0?
Math.round(8.5)
Math.rint(8.5)
Math.ceil(8.5)
Math.floor(8.5)
b and d.
13.
Which
a.
b.
c.
d.
e.
of the following method returns the sine of 90 degree?
Math.sine(90)
Math.sin(90)
Math.sin(PI)
Math.sin(Math.toRadian(90))
Math.sin(Math.PI)
3
Part II. (2 pts) Fix syntax errors in the following code:
a.
public class Test1 {
public static void main(String[] args) {
double i = 1;
while (i <= 5) {
xMethod(i);
i++;
}
}
public static double xMethod(int i) {
i++;
}
}
Part III. (6 pts) Show the printout of the following code:
a.
public class Test1 {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
xMethod(i);
i++;
}
}
public static void xMethod(int i) {
int num = 1;
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num *= 3;
}
System.out.println();
}
}
b.
public class Test2 {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
xMethod(i);
i++;
}
System.out.println("i is " + i);
}
public static void xMethod(int i) {
do {
if (i % 2 != 0)
System.out.print(i + " ");
4
i--;
}
while (i >= 1);
System.out.println();
}
}
Part IV: (8 pts):
a. (6 pts) Write a method that will return the sum of all digits in an
integer. The method signature is as follows:
(for example, sum(1234) returns 10 and sum(12345) returns 15.)
public static int sum(int number)
5
b. (2 pts) Write a method that will display numbers from 1 to n. The
numbers are separated by one space. The method signature is as
follows:
public static void displayNumber(int n)
6
Key
Part I: Multiple Choice Questions: (1 pts each)
1. c
2. b
3. a
4. d
5. a
6. b
7. c
8. a
9. b
10. a
11. a
12. e
13. d
Part II. (2 pts) Fix Errors in the following code:
a.
public class Test1
{
public static void main(String[] args)
{
double i = 1;
while (i <= 5)
{
xMethod(i); // xMethod returns double
i++;
}
}
public static double xMethod(double
i)
{
i++; return i;
}
}
Part III. (9 pts) Show the printout of the following code:
a.
public class Test1
{
public static void main(String[] args)
{
int i = 1;
while (i <= 5)
{
xMethod(i);
i++;
7
}
}
public static void xMethod(int i)
{
int num = 1;
for (int j=1; j<=i; j++)
{
System.out.print(num + " ");
num *= 3;
}
System.out.println();
}
}
1
1
1
1
1
b.
3
3 9
3 9 27
3 9 27 81
public class Test1
{
public static void main(String[] args)
{
int i = 1;
while (i <= 5)
{
xMethod(i);
i++;
}
System.out.println("i is " + i);
}
public static void xMethod(int i)
{
do
{
if (i%2 != 0)
System.out.print(i + " ");
i--;
}
while (i >= 1);
System.out.println();
}
}
1
1
3 1
3 1
5 3 1
8
i is 6
Part IV: (8 pts):
a.
public static int sum(int number) {
int sum = 0;
while (number != 0) {
sum += number % 10;
number = number / 10;
}
return sum;
}
b.
public static void displayNumber(int n) {
for (int i = 1; i <= n; i++)
System.out.print(i + " ");
}
9
Download