CS 101-02 Quizzes

  1. Date: Feb. 14, 2017
    Question a) Which of the following are not valid identifiers? Explain.
    	sinif
    	6ab
    	number of seats
    	a.b
    	_a_
    	a%
    	float
    	(7)_
    	main
    	Neyin

    Answer:

    6ab: starts with a digit.
    number of seats: contains the space character
    a.b: contains the "." character
    a%: contains "%"
    floatreserved word
    (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:
    21
  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. If the BMI > 30, the person is called obese.

    Write a method that returns true if the person is obese, false otherwise.
    Answer:

    	/**
    	 * Checks if a person is obese
    	 * @param weight the weight of the person, in kg
    	 * @param height the height of the person, in m
    	 * @return true if the person is obese
    	 */
    	public static boolean obese(double weight, double height) {
    		return weight/height/height > 30;
    	} // obese
    

  3. Date: Mar. 24, 2017
    Question Write a method, called countUpperCase, that takes a string as irs argument and returns the number of upper case characters in the argument.
    Answer:

    	/**
    	 * Computes the number of upper case characters in its argument string.
    	 * @param str in which the number of upper case characters is to be counted
    	 * @return the number of aupper case characters
    	 */
    	public static int countUpperCase(String str) {
    		int count = 0;
    		for (int i=0; i < str.length(); i++)
    			if (Character.isUpperCase(str.charAt(i)))
    				count++;
    		return count;
    	} // countUpperCase
    

  4. Date: Mar. 31, 2017
    Write a method, called displayDoubleArray, that takes a double array and a size, as its parameneters, then displays the first size many elements of the array, nicely. If the length of the array is less than or equal to the size, your method should display all of the elements of the array.
    Answer:

    	/**
    	 * .Displays the first size elemets of the double array
    	 * @param da, doble array
    	 * @param size
    	 */
    	public static void displayDoubleArray (double[] da, int size) {
    		if (size > da.length) size = da.length; // size = size > da.length ? da.length : size;
    		System.out.print("{" + (size > 0 ? da[0] : ""));
    		for (int i=1; i < size; i++)
    			System.out.print(", "+da[i]);
    		System.out.println("}");
    	} // displayIntArray
    

  5. Date: Apr. 14, 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 program that creates a copy of a text file. Your program should get the name of an existing file from the user. Then, it should create a copy of that file and save into the same directory. Your program should get the name of the copy file form the user also.
    Sample run:
    Enter the name of the file to read: original.txt
    Enter the name of the file to write: copy.txt

    Answer:

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Q6_2 {
    
    	public static void main(String[] args) throws IOException{
    		Scanner kb = new Scanner(System.in);
    		
    		System.out.print("Enter the name of the file to read: ");
    		String inFileName = kb.next();
    		File inFile = new File (inFileName);
    		Scanner in = new Scanner (inFile);
    
    		System.out.print("Enter the name of the file to write: ");
    		String outFileName = kb.next();
    		PrintWriter out = new PrintWriter (outFileName);
    		
    		String line;
    		while (in.hasNextLine()) {
    			line = in.nextLine();
    			out.println(line);
    		} // while
    		n.close();
    		out.close();
    	} // main
    } // class  Q6_2
    

  7. Date:May 5, 2017
    Question:What is the output of the following program?
    import java.util.Scanner;
    public class Quiz7_2 {
    
    	public static void main(String[] args) {
    		String text1 = "xyz";
    		Scanner sc1 = new Scanner(text1);
    		sc1.useDelimiter("");
    		while (sc1.hasNext()) System.out.println(sc1.next());
    		String text2 = "12 abr \t 12.7 false \t 140";
    		Scanner sc2 = new Scanner(text2);
    		while (sc2.hasNext())
    			if(sc2.hasNextByte())
    				System.out.println(3 + sc2.nextByte());
    			else if(sc2.hasNextDouble())
    				System.out.println(2 + Double.parseDouble(sc2.next()));
    			else
    				System.out.println(sc2.next());
    	} // main()
    } // class
    

    Answer:

    x
    y
    z
    15
    abr
    14.7
    false
    142.0
    

  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()
    }