[ Quiz Answers | Chapter Index | Main Index ]

Quiz on Chapter 4

This page contains questions on Chapter 4 of Introduction to Programming Using Java. You should be able to answer these questions after studying that chapter. Sample answers to these questions can be found here.

Question 1:

A "black box" has an interface and an implementation. Explain what is meant by the terms interface and implementation.

Question 2:

A subroutine is said to have a contract. What is meant by the contract of a subroutine? When you want to use a subroutine, why is it important to understand its contract? The contract has both "syntactic" and "semantic" aspects. What is the syntactic aspect? What is the semantic aspect?

Question 3:

Briefly explain how subroutines can be useful in the top-down design of programs.

Question 4:

Discuss the concept of parameters. What are parameters for? What is the difference between formal parameters and actual parameters?

Question 5:

Give two different reasons for using named constants (declared with the final modifier).

Question 6:

What is an API? Give an example.

Question 7:

What might the following expression mean in a program?

(a,b) -> a*a + b*b + 1

Question 8:

Suppose that SupplyInt is a functional interface that defines the method public int get(). Write a lambda expression of type SupplyInt that gets a random integer in the range 1 to 6 inclusive. Write another lambda expression of type SupplyInt that gets an int by asking the user to enter an integer and then returning the user's response.

Question 9:

Write a subroutine named "stars" that will output a line of stars to standard output. (A star is the character "*".) The number of stars should be given as a parameter to the subroutine. Use a for loop. For example, the command "stars(20)" would output

********************

Question 10:

Write a main() routine that uses the subroutine that you wrote for Question 7 to output 10 lines of stars with 1 star in the first line, 2 stars in the second line, and so on, as shown below.

*
**
***
****
*****
******
*******
********
*********
**********

Question 11:

Write a function named countChars that has a String and a char as parameters. The function should count the number of times the character occurs in the string, and it should return the result as the value of the function.

Question 12:

Write a subroutine with three parameters of type int. The subroutine should determine which of its parameters is smallest. The value of the smallest parameter should be returned as the value of the subroutine.

Question 13:

Write a function that finds the average of the first N elements of an array of type double. The array and N are parameters to the subroutine.

Question 14:

Explain the purpose of the following function, and explain how it works:

static int[] stripZeros( int[] list ) {
    int count = 0;
    for (int i = 0; i < list.length; i++) {
        if ( list[i] != 0 )
            count++;
    }
    int[] newList;
    newList = new int[count];
    int j = 0;
    for (int i = 0; i < list.length; i++) {
        if ( list[i] != 0 ) {
            newList[j] = list[i];
            j++;
        }
    }
    return newList;
}

[ Quiz Answers | Chapter Index | Main Index ]