CS 101-01 Quizzes

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

  2. Date: Feb. 18, 2013
    Question: What is the output of the following program segment?
    	int x=5;
    	float f = (float) 6.78;
    	System.out.println("x is "+ x+"\n\t\b"+f+" meters");
    	System.out.println("The results is shown above\ris reverse");
    

    Answer:
    x is 5
    6.78 meters
    is reverse is shown above
    
    See the sample code.
  3. Date: Feb. 25, 2013
    Question: Write a complete java program that will read the number of students in three sections of CS 101 class separately and display the average number o stdents in a section.
    Answer:
    /**
     * @(#)Q_01_3.java
     *
     * @author H. Altay Güvenir
     * @version 1.00 2013/2/25
     */
    import java.util.Scanner;
    
    public class Q_01_3 {
    	public static void main (String[] args) {
    		Scanner keyboard = new Scanner (System.in);
    
    		int s1; // Number of students in Section 1
    		System.out.print ("Enter the number of students in section 1: ");
    		s1 = keyboard.nextInt(); // read s1 from the keyboad
    
    		int s2; // Number of students in Section 2
    		System.out.print ("Enter the number of students in section 2: ");
    		s2 = keyboard.nextInt(); // read s2 from the keyboad
    
    		int s3; // Number of students in Section 3
    		System.out.print ("Enter the number of students in section 3: ");
    		s3 = keyboard.nextInt(); // read s3 from the keyboad
    
    		double average = (s1+s2+s3)/3.0; // compute the average
    		System.out.println ("Average number of students in a section is "+ average);
    	} // main
    } // class Q_01_3
    
    See the sample code.
  4. Date: Mar. 6, 2013
    Question: Define shirt sizes as an enumerated type. Declare the shirt sizes of your three friends. Assign proper values to these variables. Display the values the values of these variables with appropriate information. Also display the ordinal values of these values.
    Answer:
    /**
     * @(#)Q_01_4.java
     *
     * @author H. Altay Güvenir
     * @version 1.00 2013/3/6
     */
    
    public class Q_01_4 {
    	
    	enum ShirtSize {extraSmall, small, medium, large, extraLarge, extraExtraLarge};
    	
    	public static void main (String[] args) {
    		ShirtSize narinSize, jaleSize, tosunSize; // shirt sizes of three friends
    
    		narinSize = ShirtSize.extraSmall; 	// Hale wares extra small size shirts
    		jaleSize  = ShirtSize.small;		// Jale wares small size shirts
    		tosunSize = ShirtSize.extraLarge;	// Tosun wares large size shirts
    
    		System.out.println("Shirt size for Narin is "+ narinSize+".");
    		System.out.println("Shirt size for Jale is " + jaleSize+".");
    		System.out.println("Shirt size for Tosun is "+ tosunSize+".");
    
    		// Displaying the ordinal values of the variables of ShirtSize type
    		System.out.println ("Ordinal value of "+narinSize+" is "+narinSize.ordinal());
    		System.out.println ("Ordinal value of "+jaleSize+" is "+jaleSize.ordinal());
    		System.out.println ("Ordinal value of "+tosunSize+" is "+tosunSize.ordinal());		
    	} // main
    } // class Q_01_4
    
    See the sample code.
  5. Date: Apr. 3, 2013
    Question: What is the output of the following program?
    public class Q_01_5 {
    	
    	public static void main(String[] args) {
    		int x, y, z;
    		x = 5;
    		z = 0;
    		while (x-- > 0) {
    			y = x + 2;
    			while (y > 0)
    				  System.out.println(y--%2==0?z++:z--);	
    		} // while	
    		System.out.println(z);	
    	} // main
    }  // class Q_01_5
    

    Answer:
    0
    1
    0
    1
    0
    1
    0
    -1
    0
    -1
    0
    -1
    0
    -1
    0
    -1
    -2
    -1
    -2
    -1
    -2
    
    See the sample code.
  6. Date: Apr. 10, 2013
    Question: Given the bottleList, which is an ArrayList of WaterBottle objects, write a program segment that prints the brandName of the WaterBottle object whose capacity is the minimum.
    Answer:
    	WaterBottle minBottle = bottleList.get(0);
    	float minCap = minBottle.getCapacity();
    		
    	// Using a for-each loop
    	for (WaterBottle wb: bottleList)
    		if (wb.getCapacity() < minCap) {
    			minBottle = wb;
    			minCap = minBottle.getCapacity();
    		}
    	System.out.println(minBottle.getBrandName());
    
    See the sample code.
  7. Date: Apr. 17, 2013
    Question: Write a method to compute ex using its Talor series definition as
    .

    Answer:
    	private static int factorial (int n) {
    		int result = 1;
    		for (int i=2; i<=n; i++)
    			result *= i;
    		return result;
    	} // factorial
    	
    	public static double e (double x) {
    		double sum =0;
    		for (int k=0; k<20; k++)
    			sum += Math.pow(x, k) / factorial(k);
    		return sum;
    	} // e
    
    See the sample code.
  8. Date: May 06, 2013
    Question: Write a method, called find, which takes an int array, called ar, and another int value, called key. It returns the index of key in ar. If the key does not exist in ar, it returns -1.
    Answer:
    	public static int find (int[] ar, int key) {
    		for (int i=0; i < ar.length; i++)
    			if ( key== ar[i] )
    				return i;
    		return -1;
    	} // find
    
    See the sample code.
  9. Date: May 08, 2013
    Question: Write a method, that takes a double[][] as its parameter and returns the sum of values in the array.
    Answer:
    	public static double sum2D (double[][] da) {
    		double sum = 0;
    		for (int r=0; r < da.length; r++)
    			for (int c=0; c<da[r].length; c++)
    				sum += da[r][c];
    		return sum;
    	} // sum2D
    
    See the sample code.