CS 101-03 Quizzes

  1. Date: Feb. 12, 2013
    Question: Which of the following words are NOT java identifiers? Explain.
    Answer:

  2. Date: Feb. 19, 2013
    Question: What is the output of the following program segment?
    	int i = 7;
    	double d = 2.0;
    	boolean flag = true;
    	System.out.println("Value of i "+i);
    	System.out.println("Value of d/i is \rd/i");
    	System.out.println(flag+"\rg");
    	System.out.println("This is \n\tthe last line\b\b?");
    

    Answer:
    Value of i 7
    d/iue of d/i is
    grue
    This is
            the last li?e
    
    See the sample code.
  3. Date: Feb. 22, 2013
    Question: What is the output of the following program segment?
    	int a = 5;
    	System.out.println(++a);
    	System.out.println(a);
    	a = 7;
    	System.out.println(++a + a++);
    	System.out.println(a);
    

    Answer:
    6
    6
    16
    9
    
    See the sample code.
  4. Date: Feb. 26, 2013
    Question: Write a complete java program that reads ages of four persons from the keyboard and displays the average age of those people.
    Answer:
    /**
     * @(#)Q_03_4.java
     *
     * @author H. Altay Güvenir
     * @version 1.00 2013/2/26
     */
    import java.util.Scanner;
    
    public class Q_03_4 {
    	public static void main (String[] args) {
    		int totalAge = 0; // totalAge will store the total of the ages of the people
    		
    		Scanner keyboard = new Scanner(System.in); // Declare the keyboard
    
    		System.out.print("Enter the age of the first person: ");
    		totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
    		
    		System.out.print("Enter the age of the second person: ");
    		totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
    		
    		System.out.print("Enter the age of the third person: ");
    		totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
    		
    		System.out.print("Enter the age of the fourth person: ");
    		totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
    		// Now, totalAge contains the sum of the ages of the four people
    		
    		// Display the average age
    		System.out.println ("Their average is "+ totalAge/4.0);
    	} // main
    } // class Q_03_4 
    
    See the sample code.
  5. Date: Apr. 2, 2013
    Question: What is the output of the following program?
    public class Q_03_5 {
    
    	public static void main (String[] args) {
    		int count = 0;
    		int i = 4;
    		int j;
    
    		while (i > 0) {
    			j = i;
    			while (j-- > 0)
    				count ++;
    			i--;
    		} // outer while
    
    		// What is the output generated by the following statement?
    		System.out.println (count);
    	} // main
    } // class Q_01_5
    

    Answer:
    10
    
    See the sample code.
  6. Date: Apr. 5, 2013
    Question: Re-Write the following if-else statement using the switch statement.
    	if (harf == 'A')
    		System.out.println("Very Good");
    	else if (harf == 'B')
    		System.out.println("Good");
    	else if (harf == 'C')
    		System.out.println("OK");
    	else
    		System.out.println("Not Good");
    

    Answer:
    	switch (harf) {
    	case 'A' : 
    		System.out.println ("Very Good");
    		break;
    	case 'B' :
    		System.out.println ("Good");
    		break;
    	case 'C' :
    		System.out.println ("OK");
    		break;
    	default:
    		System.out.println ("Not Good");
    	} // switch
    
    See the sample code.
  7. Date: Apr. 9, 2013
    Question: Given the following do-while statement, write and equivalent for statement.
    	do {
    		System.out.print("Enter your age: ");
    		age = kb.nextInt();
    	} while (age<0 || age>120);
    

    Answer:
    	for (age = -1; age < 0 || age > 120; ) {
    		System.out.print("Enter your age: ");
    		age = kb.nextInt();
    	}	
    
    See the sample code.
  8. Date: Apr. 19, 2013
    Question: Write a method, called combination, that takes two int values, called m and n, and returns the value of C(m,n). Note, assume m > n.
    Answer:
    	private static int factorial (int n) {
    		int result = 1;
    		for (int i=2; i<=n; i++)
    			result *= i;
    		return result;
    	} // factorial
    		
    	public static int combination (int m, int n) {
    		return factorial(m) / factorial(n) / factorial(m-n);
    	} // combination	
    
    See the sample code.
  9. Date: Apr. 30, 2013
    Question: Write a method, called sum, that takes a double array, called d, as its formal parameter, and returns the sum of all values in the array, d.
    Answer:
    	public static double sum(double[] doubleArray) {
    		double sum = 0;
    		for (double d: doubleArray)
    			sum += d;
    		return sum;
    	} // sum
    
    See the sample code.