1.Suppose a method is declared as:

public static void f (double d, int n) {
....;
}

Which one of the follow method invocations would compile with no errors?

Select one:
a. f(2.5, 3.5);
b. f(2, 3);
c. f(2.5);
d. f(3);

I chose b

2.Each of the following pieces of information is spelled out by the header of a method. Which one is of no importance to a caller of the method?

Select one:
a. The names of the formal parameters
b. The types of the parameters the method takes
c. The type of value returned by the method, if any
d. The number of parameters the method takes
e. The name of the method

I chose b

3.Consider this method:

public static int f (int a, int b, int c) {
return (a+b+c)/3;
}

What is the value of the expression f(1,2,3)?

I got 2.

4.How many parameters does the Math.copySign method take? (Feel free to browse the documentation to find the answer.)
Select one:
a. 0
b. 3
c. 2
d. 1

I chose c

5.If a method is invoked as s.m(3) and s is an object, then m is a static method.

Select one:
True
False

I chose true

6.How many times will the loop execute its body?

int x = 1;
while (x < 100) {
System.out.println(x + " ");
x += 10;
}

Select one:
a. 11
b. 10
c. 8
d. 9

I chose d

7.How many times will the loop execute its body?

int x = 10;
while (x % 3 != 0) {
System.out.println(x);
}

Select one:
a. more than a billion
b. 9
c. 0
d. 10

I chose a.

8.How many times will the loop execute its body?

String word = "a";
while (word.length() < 10) {
word = "b" + word + "b";
}

Select one:
a. 0
b. 7
c. 5
d. 6

I chose b

9.How many times will the loop execute its body?

int x = 20;
while (x > 0) {
System.out.println(x/10);
x = x / 2;
}

Select one:
a. 5
b. 2
c. more than a billion
d. 10

I chose b

10.Which of the following statements is false?

Select one:
a. A formal parameter is a variable
b. An actual parameter is an expression
c. The number of actual parameters must equal the number of formal parameters
d. The variables used as actual parameters must have the same names as the The variables used as actual parameters must have the same names as the variables used as formal parameters

I chose c

You can test a lot of these by writing programs and trying them out. You list the question and give your answer, but not your rationale.

For example...

Suppose a method is declared as:

> public static void f (double d, int n) {
> ....;
> }

Which one of the follow method invocations would compile with no errors?

Select one:
a. f(2.5, 3.5);
b. f(2, 3);
c. f(2.5);
d. f(3);

I chose b

You are right, but why are you right, or better yet, why are a, c, and d wrong?

Right off the bat, you can eliminate c and d. They have the wrong number of parameters. It's between a and d. One has two ints, one has two doubles. The function wants one int and one double. So how do you solve it? Well, when a function wants a double and is passed a double, it turns the int into a double. That's no problem because 2 turns into 2.0 quite nicely. The opposite is not true. A function that wants an int can't take 2.5 and turn it into an int. Hence the answer is b.

2.Each of the following pieces of information is spelled out by the header of a method. Which one is of no importance to a caller of the method?

Select one:
a. The names of the formal parameters
b. The types of the parameters the method takes
c. The type of value returned by the method, if any
d. The number of parameters the method takes
e. The name of the method

I chose b

Wrong here. See above The parameter types are important, as explained above. The names do not matter...

int foo(int a, double b);
int foo(int c, double d);

These are the same as far as the calling function is concerned, so the answer is going to be a. "foo" declares what it needs to do its job. How it does it and what names it gives its internal variables is irrelevant to the calling function.

Give 3 through 9 a shot by writing programs. If you're still confused afterwards, post again with a more detailed question.

For number 10, see the example in number 1...

f(2.5, 2.5);

2.5 and 3.5 don't have names. Look at your choices and see if that clarifies the answer

You chose c. If you chose c on number 10, why did you not choose d for number 2?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.