Question 2: Answer the following questions: (30 marks) 1. What are two ways to include comments? state when we use each of them? a. block comments /* */ : is used for multiple lines comment b. line comments,inline comments // : is used for single line comment 2. Write down two main differences between setter and getter methods? Setter Getter 1).usually don t have a return value 1) Must have a return value. 2) have an effect on the state of the object 2) cannot alter the state of the object. 3. What happens if the code throws an exception that is not handled in any try catch statement within the method? Exception propagation 4. Define classes name clash in Java? Suggest one solution for this problem? Name Clash: two classes with the same name in different packages. Solution: If you need two classes from different packages with the same name you have to use the fully qualified class name.
Question 3: write the suitable java statement or code segment from (a-d) questions (10 marks) a. Declare an array named arr of type double with 7 elements. double arr[]=new double [7]; b. Fill the array arr, use the Scanner class to read the input from the keyboard. Scanner input=new Scanner(System.in); for(int i=0;i<arr.length ; i++) arr[i]= input.nextdouble; c.display the number of even elements in the array arr. int counter=0; for(int i=0;i<arr.length ; i++) if(arr[i]%2 == 0) counter++; System.out,println( The number of even elements is +counter); d. Write the suitable import statement to be able to use the Scanner class? Import java.util.scanner;
Question 4: 1. find the output from the following java code (5 marks) : public static void main(string[] args) throws ArithmeticException int x=6; int y=3; double z; for(int i=1;i<=4;i++) try z=x/y; System.out.println("the result is"+z); y--; Output: the result is2.0 the result is3.0 the result is6.0 Division by zero catch (ArithmeticException e) System.out.println("Division by zero"); catch(exception e) 2. Answer the questions related to the following: (8 marks) System.out.println("Error in Division");
2. Use the given Sclass definition and a Test program for it to answer the questions a&b.( marks) public class Test public static void main(stirng args[]) Sclass s1=new Sclass(); s1.inc(); s1.printz(); s1.printw(); Sclass.inc(); s1.printz(); Sclass s2=new Sclass(); s2.inc(); s2.printw(); s1.printz(); s2.printz(); s2.printw(); public class Sclass private static int z; private int w; public static void printz() System.out.println("the value of z "+z); public void printw() System.out.println("the value of w "+w); public static void inc( ) z++; a) Find the output Output the value of z 1 the value of w 0 the value of z 2 the value of w 0 the value of z 3 the value of z 3 the value of w 0 a) If we add the statement System.out.println("the value of w "+w) to the printz() method then the result is : Compilation error, we can t refer a non static variable from a static method
Question5: given below the definition for class Box and class MatchBox, with a test program. Answer the questions form a-j. public class Box a. Declare 3 private variables of type double (width, height and depth) private double width; private double height; private double depth; b. Write a constructor with three parameters to initialize the class variables with them Box(double w, double h, double d) width = w; height = h; depth = d; c. Write a method named getw to return the width public double getw()return width; // a method that will calculate the volume for the Box public double getvolume() return width*height*depth; public String tostring() return the width is +width + \n + the height is +height+ \n + the depth is +depth; public class MatchBox extends Box public double weight; d. Declare the suitable no arguments constructor for the class MatchBox that sets its class variables to the default value. MatchBox( ) super(0,0,0); weight = 0; e. Override the method getvolume in the superclass Box to return the square value for the weight. public double getvolume() return weight*weight; f. Override the tostring method in class Box to print the suitable variables for class MatchBox. public String tostring() return super.tostring()+ the weight is + weight
public static void main(string args[]) g. Declare an object named b1 of type Box Box b1 = new Box(3,5,10); h. Declare an object named mb1 of typematchbox MatchBox mb1 = new MatchBox(); i. Display the volume for object b1. System.out.println(b1.getVolume()); j. Display the object mb1 System.out.println(mb1); Question 6: write a simple java program that reads 10 integers form a file named Data.txt then print the number of occurrences for number 5. public static void main(string[] args) int a; File f= new File("C:\\ Data.txt") ; Scanner input=new Scanner(f); a=input.nextint(); int counter=0; for(int i=1; i<=10;i++) a=input.nextint(); if(a==5) Counter++; System.out.println(counter); input.close();