CS 101-01 Quizzes

  1. Date: Feb. 14, 2017
    Question a) Which of the following are not valid identifiers? Explain.
    	myClass
    	class
    	6ab
    	int
    	main
    	[ab]
    	%a
    	ab+7
    	Class
    	(7)

    Answer:

    class: reserved word.
    6ab: starts with a digit.
    int: reserved word.
    [ab]: contains "[" and "]"
    %a: contains "%"
    ab+7: contains "+"
    (7): contains "(" and ")"

    Question b) What is the output of the following program?

    public class Q1 {
    	public static void main (String[] args) {
    		int a = 3;
    		int b = a + 4;
    		b = b + a;
    		System.out.println(b);
    	}
    }

    Answer:
    10
  2. Date: Mar. 21, 2017
    Question: Body Mass Index (BMI) is defined as weight / height2, where weight is in kg and height is in m.

    Write a method that returns the BMI of a person. It takes the weight and height in kg and m, respectively. Assume that the actual parameters have valid values.

    A simple call would be

    	aBMI = bmi(81.2, 1.73);

    Answer:

    	/**
    	* Computes the BMI of a person
    	* @param weight the weight of the person, in kg
    	* @param height the height of the person, in m
    	* @return the BMI
    	*/
    	public static double bmi(double weight, double height) {
    		return weight/height/height;
    	} // bmi
    

  3. Date: Mar. 23, 2017
    Question: Write a method, called countDigits, that takes a string argument, and returns the number of digits in the argument. Write its javadoc comments, as well.

    For example, for the string "ab7Ag627", it returns 4.
    Answer:

    	/**
    	 * Counts the number of digits in a string
    	 * @param str the string
    	 * @return the number of digits in a str
    	 */
    	public static int countDigits(String str) {
    		int count = 0;
    		for (int i=0; i < str.length(); i++)
    			if (Character.isDigit(str.charAt(i)))
    				count++;
    		return count;
    	} // countDigits
    

  4. Date: Mar. 30, 2017
    Question: Write a method, called initializeIntArray, that takes two arguments; the first arguments is and int[] and the second is int. This method sets the value of each element of the array to the second argument. Write its javadoc comment, also.
    Answer:

    	/**
    	* Sets the value of each element of the array to the second argument.
    	* @param array, array to be initialized
    	* @param value, to be the value of each element
    	*/
    	public static void initializeIntArray(int[] array, int value) {
    		for (int i=0; i < array.length; i++)
    			array[i] = value;
    	} // initializeIntArray
    
    A sample call:
    	public static void main(String[] args) {
    		int[] a = new int[10];
    		initializeIntArray(a, 5);
    	}
    

  5. Date: Apr. 13, 2017
    Question:Class Watch is defined as below:
    public class Watch {
    	private boolean digital;
    	private static int hours = 12;
    	private int minutes;
    	private String color = "Gray";
    	
    	public Watch (){
    	}
    	
    	public Watch (int hours){
    		this.hours = hours;
    	}
    	
    	public Watch (int hours, int minutes){
    		this.hours = hours;
    		this.minutes = minutes;
    	}
    	
    	public Watch (String color){
    		this.color = color;
    	}
    		
    	public int getHours () {
    		return hours;
    	} // getHours
    	
    	public void setHours(int hours) {
    		this.hours = hours;
    	} // setHours
    	
    	public int getMinutes () {
    		return minutes;
    	} // getMinutes
    	
    	public void setMinutes(int minutes) {
    		this.minutes = minutes;
    	} // setMinutes
    	
    	public String getColor () {
    		return color;
    	} // getColor
    	
    	public void setColor(String color) {
    		this.color = color;
    	} // setColor
    } // Watch
    What is the output of the following code segment?
    	public static void main(String[] args) {
    		Watch myW1 = new Watch(1, 2);
    		Watch yourW1 = new Watch(3);
    		Watch herW1 = new Watch("Pink");
    		System.out.println(myW1.getHours()+" "+myW1.getMinutes());
    		System.out.println(yourW1.getHours()+" "+yourW1.getMinutes());
    		System.out.println(herW1.getHours()+" "+herW1.getMinutes());
    	}

    Answer:

    3 2
    3 0
    3 0

  6. Date:May 2, 2017
    Question:Write a complete program that opens a text file whose name is "original.txt", and creates its copy into a file whose name is "copy.txt".
    Answer:

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Q6_1 {
    
    	public static void main(String[] args) throws IOException {
            File inFile = new File ("original.txt");
            Scanner in = new Scanner (inFile);
            PrintWriter out = new PrintWriter ("copy.txt");
            String line;
            while (in.hasNextLine()) {
            	line = in.nextLine();
            	out.println(line);
            } // while
            in.close();
            out.close();
    	} // main
    } // class  Q6_1
    

  7. Date:May 4, 2017
    Question:What is the output of the following program?
    import java.util.Scanner;
    public class Quiz7_1 {
    
    	public static void main(String[] args) {
    		String text1 = "123";
    		Scanner sc1 = new Scanner(text1);
    		sc1.useDelimiter("");
    		while (sc1.hasNext()) System.out.println(sc1.next());
    		String text2 = "12 abr \t 12.75 false \n ABC";
    		Scanner sc2 = new Scanner(text2);
    		while (sc2.hasNext())
    			if(sc2.hasNextDouble())
    				System.out.println(2 * Double.parseDouble(sc2.next()));
    			else
    				System.out.println(sc2.next());
    	} // main()
    } // class

    Answer:

    1
    2
    3
    24.0
    abr
    25.5
    false
    ABC
    

  8. Date:May 9, 2017
    Question:Write a program that takes any number of integers from the command line and displays their sum.
    Answer:

    public class Sum {
    	public static void main(String[] args) {
    		int sum =0;
    		for (String a: args)
    			sum += Integer.parseInt(a);
    		System.out.println(sum);
    	} // main()
    }