Page 1 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, , Chapter 2, , Revision Tour II, Mental Drill, A. Tick () the correct option., 1., , Which among the following is not a valid error in Java?, a. Syntax errors, , b. Logical errors, , c. Run-time errors, Ans. d. Technical errors, 2., , d. Technical errors, , Which among the following Scanner methods allows us to input a number with a decimal point?, a. nextInt(), , b. nextFloat(), , c. nextDecimal(), Ans. b. nextFloat(), 3., , d. nextPoint(), , The output in BlueJ occurs in which window?, a. Console window, , b. Terminal window, , c. Both a and b, Ans. b. Terminal window, 4., , d. None of these, , The input in BlueJ occurs in which window?, a. Console window, , b. Terminal window, , c. Both a and b, Ans. b. Terminal window, 5., , d. None of these, , Assigning value to a variable during declaration is called., a. Declaration, , b. Assignment, , c. Initialisation, Ans. c. Initialisation, , d. None of these, , 6. Which among the following is used to represent single-line comment?, a. //, Ans. a. //, 7., , b. /*, , c. \\, , d. <!—, , Which among the following is a logical error?, a. Missing semicolon, , b. Mismatched braces in classes and methods., , c. Misspelled keywords and identifiers., d. Addition is required but subtraction is performed., Ans. d. Addition is required but subtraction is performed., Computer Applications – X (ICSE Course) Answers, , 18
Page 2 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, , 8., , Which among the following represents a syntax error?, a. Dividing an integer by zero., b. Accessing an element that is out of bounds of an array., c. Trying to store a value which is incompatible to a certain data-type., d. Missing semicolon, , Ans., 9., , d. Missing semicolon, , If the data that is to take part in a calculation in a method is fixed, which form of input is, necessary?, a. Initialisation, , b. Parameterised input, , c. Scanner input, , d. None of these, , Ans., , a. Initialisation, , 10. In case you need to give a proper message (prompt string) before taking an input from the user,, which would be the most preferable method?, a. Parameterised input, , b. Initialisation, , c. Scanner input, , d. None of these, , Ans., B., , C., , c. Scanner input, , Fill in the blanks., 1., , The next().chart(0) function of the Scanner class is used to accept a character from the user., , 2., , The relational/comparison operator is used to compare two quantities., , 3., , The else block is preceded by the if block., , 4., , The sqrt() function of the Math class is used to return the square root of a number., , 5., , The return type of cbrt( ) function is double., , 6., , Single line comment and multiline comments are two types of comments in Java., , 7., , Any error in the grammar of the language is a syntax error., , State whether the following statements are True (T) or False (F)., 1. Scanner class is present in the java.lang package. F, 2. Math.abs( ) is used to find the absolute value of a number. T, 3. The return type of Math.sqrt( ) function is float. F, 4. The fraction 1/2 will evaluate to 0.5. F, , SECTION A, Answer the following questions., 1., , Name the functions of the Scanner that is used to:, (i) Accept a number of long data type
Page 3 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, (ii) Accept a group of alphabets., Ans. (i) nextLong(), 2., , (ii) nextLine(), , What is a bug? What is debugging?, , Ans. An error in a program is called a bug and the process of removing it is called debugging., 3., , What are the different types of errors that may occur in a Java program?, , Ans. Syntax Error, Logical Error and Run-time Error., 4., , What are syntax errors? Give two examples., , Ans. A syntax error is an error in the syntax of a sequence of characters or tokens that is intended to, be written in a particular program., Examples:, • Mismatched braces in classes and methods, • Wrongly accessing variable, whose visibility doesn’t exist, • Misspelled keywords and identifiers, 5., , What are run-time errors? Give two examples., , Ans. An error that occurs during the execution of a program is called run time error., Example:, • Dividing an integer by zero., • Accessing an element that is out of bounds of an array., • Trying to store a value which is incompatible to a certain datatype., 6., , What are comments? Name the different types of comments used in Java., , Ans. A comment is a programmer-readable explanation or annotation in the source code of a, computer program., Types: Single-line comment and Multiline comment, 7., , What are conditional statements? With which commands do you implement conditional, statement in Java?, , Ans. Conditional construct are specific statements that allow us to check a condition and execute, certain parts of code depending on whether the condition is true or false., Implementation is done using: if, if-else, if-else if-else, switch-case., 8., , When are braces optional or necessary in conditional statements?, , Ans. Giving braces is optional for if/else in case there is only one statement to be executed., 9., , What is the difference between the Scanner class functions next() and nextLine()?, , Ans. next() can read the input only till the space. It can’t read two words separated by space. Also,, next() places the cursor in the same line after reading the input. nextLine() reads input including, space between the words (that is, it reads till the end of line \n)., 10., , What are relational operators?, , Ans. The relational operator allows you to test or define some kind of relation between two entities., 11., , State the difference between = and = =.
Page 4 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, Ans. In Java, “=” is the assignment operator and “==” is a comparison operator., 12., , What are logical operators? Explain each of them with the help of an example., , Ans. The logical operators are used to combine simple relational statements into more complex, expressions., , 13., , Name two jump statements and their use., , Ans. Jump statements in Java:, break- The break statement causes an immediate exit from the do-while, for, switch or while, statement in which it appears., continue- The continue statement is used with the loop instructions do-while, for, and while., It transfers control to the place in the program where the next iteration of the loop in which it, appears, begins., 14., , What is an exception?, , Ans. An exception is a run-time error that occurs in a program., 15., , Rewrite the following using ternary operator:, if (bill >10000 ), discount = bill * 10.0/100;, else, discount = bill * 5.0/100;, Ans. discount=(bill >10000 ) ? bill * 10.0/100: bill * 5.0/100;, , 16., , Rewrite the following program segment using the if .. else statement., comm = (sale>15000)?sale*5/100:0;, , Ans., if (sale>15000), comm=sale*5/100, else, comm=0;, 17., , Rewrite the following using ternary operator :, if(x%2==0), System.out.print(“EVEN”);, else, System.out.print(“ODD”);
Page 5 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, Ans., System.out.print((x%2==0)?”EVEN”:“ODD”);, 18., , Name the types of error (syntax, runtime or logical error) in each case given below:, i. Division by a variable that contains a value of zero., ii. Multiplication operator used when the operation should be division., Missing semicolon., , i. Runtime error, ii. Logical error, iii. Syntax error, , 19., , Give the output of the following program segment:, , Ans., Output:, 2.0, 3.0, , 20., , What are the final values stored in variables x and y below?, double a = - 6.35;, double b = 14.74;, double x = Math.abs(Math.ceil(a));, double y = Math.rint(Math.max(a,b));, Ans. x=6.0 and y=15.0, , 21., , What are the values stored in variables r1 and r2:, (i) double r1 = Math.abs(Math.min(-2.83,-5.83));, (ii) double r2 = Math.sqrt(Math.floor(16.3));, Ans. r1=5.83 and r2=4.0, 22., , Rewrite the following program segment using if-else statements instead of the ternary, operator., String grade=(mark>=90) ? “A” : (mark>=80) ? “B” : “C”;, , Ans., String grade;, If(marks>=90), grade=“A”;, else, grade=“B”;, 23., , What are the values of a and b after the following function is executed, if the values passed
Page 6 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, are 30 and 50:, void paws(int a, int b), {, a = a + b;, b = a – b;, a = a – b;, }, Ans.a=50 and b=30, 24., , Ans. a), b), c), d), e), f), g), h), i), j), k), l), m), n), o), p), 25., , Give the output of the following:, a) Math.floor (-4.7), b) Math.ceil(3.4) + Math.pow(2, 3), c) Math.floor(-126.349), d) Math.max(45.6,17.3), e) Math.min(-0.0,0.0), f) Math.pow(4,3), g) Math.sqrt(625), h) Math.cbrt(125), i) Math.max(11,11), j) Math.ceil(-12.56), k) Math.floor(15.36), l) Math.round(146.5), m) Math.max(-17, -19), n) Math.ceil(7.8), o) Math.ceil(4.2), p) Math.abs(-4), -5.0, 12.0, -127.0, 45.6, -0.0, 64.0, 25.0, 5.0, 11, -12.0, 15.0, 147, -17, 8.0, 5.0, 4, , Write equivalent Java expressions for the following:
Page 7 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, Ans., a., b., c., d., , Math.sqrt(a+b), 1/3.0+Math.pow(a,3)+1/3.0*Math.pow(b,3), s=u*t+1/2.0*a*Math.pow(t,2), d=Math.sqrt(l*l+b*b);, e. (-b+Math.sqrt(b*b-4*a*c))/(2*a), f. (Math.pow(a,3)+Math.pow(b,3))/ (Math.pow(a,3)-Math.pow(b,3)), g. Math.abs((a-b)/(a+b)), h. Math.cbrt((a+b)/(a*b)), i. Math.pow(a+b.n)/Math.sqrt(3+b), j. z=(5*x*x*x+2*y)/(x+y);, k. Math.sqrt(2*a*s+u*u), l. Math.sqrt(3*x+x*x)/(a+b), m. (a*a+b*b)/(2*a*b), n. T=Math.sqrt(A*A+B*B+C*C);, o. a*Math.pow(x,5)+b*Math.pow(x,3)+c, , SECTION B, Programming Questions, 1., , Write a program to input the area of a square and find its perimeter., , Ans. import java.util.*;, class Sol1, {, static void main(), {, Scanner sc=new Scanner(System.in);, double a,s,p;, System.out.println(“Enter the area of a square:”);, a=sc.nextDouble();, s=Math.sqrt(a);, p=4*s;, System.out.println(“Perimeter=”+p);, }, }, 2., , Write a program to input the length and breadth of a rectangle and find its diagonal., , Ans. import java.util.*;, class Sol2, {, static void main(), {, Scanner sc=new Scanner(System.in);
Page 8 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, double l,b,d;, System.out.println(“Enter the length and breadth of the rectangle:”);, l=sc.nextDouble();, b=sc.nextDouble();, d=Math.sqrt(l*l+b*b);, System.out.println(“Diagonal=”+d);, }, }, 3., , Write a program to input 2 integers and check whether both the numbers are multiples of 7, or not., , Ans. import java.util.*;, class Sol3, {, static void main(), {, Scanner sc=new Scanner(System.in);, int a,b;, System.out.println(“Enter 2 integers:”);, a=sc.nextInt();, b=sc.nextInt();, if(a%7==0 && b%7==0), System.out.println(“Both are multiples of 7”);, else, System.out.println(“Both are not multiples of 7”);, }, }, 4., , Write a program to pass 2 integer numbers as parameters. If either of the two numbers is 0,, display invalid entry and the program should end, if it is valid entry, divide the larger number, with the smaller number and display the result., , Ans., class Sol4, {
Page 9 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, static void divide(int a,int b), {, if(a==0 || b==0), System.out.println(“Invalid Entry”);, else, {, float q;, if(a>b), q=(float)a/b;, else, q=(float)b/a;, System.out.println(“Quotient:”+q);, }, }, }, 5., , Write a program to input 3 unique integers and print the smallest among them., , Ans. import java.util.*;, class Sol5, {, static void main(), {, Scanner sc=new Scanner(System.in);, int a,b,c;, System.out.println(“Enter 3 integers:”);, a=sc.nextInt();, b=sc.nextInt();, c=sc.nextInt();, if(a<b && a<c), System.out.println(“Smallest:”+a);, else if(b<a && b<c), System.out.println(“Smallest:”+b);, else, System.out.println(“Smallest:”+c);, }, }, 6., , Write a program to input the three angles of a triangle and check whether it forms a triangle, or not, if it forms a triangle, check whether it is an equilateral, isosceles or a scalene triangle., (Hint: To form a triangle, the sum of the angles should be 180 degrees., To form an equilateral triangle every angle should be equal., To form an isosceles triangle any two angles should be equal., To form a scalene triangle all three angles should be different from each other.), Ans. import java.util.*;, class Sol6, {, static void main(), {, Scanner sc=new Scanner(System.in);
Page 10 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, int a,b,c;, System.out.println(“Enter 3 angles:”);, a=sc.nextInt();, b=sc.nextInt();, c=sc.nextInt();, if(a+b+c==180), {, if(a<90 && b<90 && c<90), System.out.println(“Acute angled triangle”);, else if(a>90 || b>90 || c>90), System.out.println(“Obtuse angled triangle”);, else, System.out.println(“Right angled triangle”);, }, else, System.out.println(“Cannot form a triangle”);, }, }, 7., , Write a program to input the three sides of a triangle and check whether it forms a triangle or, not, if it forms a triangle, check whether it is an equilateral, isosceles or a scalene triangle., (Hint: To form a triangle, each side should be less the sum of the other two sides., To form an equilateral triangle every side should be equal., To form an isosceles triangle any two sides should be equal., To form a scalene triangle all three sides should be different from each other.), Ans. import java.util.*;, class Sol7, {, static void main(), {, Scanner sc=new Scanner(System.in);, int a,b,c;, System.out.println(“Enter 3 sides:”);, a=sc.nextInt();, b=sc.nextInt();, c=sc.nextInt();, if(a<b+c && b<a+c && c<a+b), {, if(a==b && b==c), System.out.println(“Equilateral triangle”);, else if(a==b || b==c || c==a), System.out.println(“Isosceles triangle”);, else, System.out.println(“Scalene triangle”);, }, else, System.out.println(“Cannot form a triangle”);, }, }
Page 11 :
VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] PH: 9336061496, , 8., , Write a program to accept three sides of a triangle as parameter and check whether it can, form a triangle or not. If it forms a triangle, check whether it is an acute angled, obtuse angled, or right-angled triangle., (Hint: To form a triangle, each side should be less the sum of the other two sides.., To form an acute angled triangle the square of every side should be less than the sum of the, squares of the other two sides., To form an obtuse angled triangle the square of any side should be greater than the sum of the, squares of the other two sides., To form an right angled triangle the square of any side should be equal to the sum of the, squares of the other two sides.), , Ans. import java.util.*;, class Sol8, {, static void main(), {, Scanner sc=new Scanner(System.in);, int a,b,c;, System.out.println(“Enter 3 sides:”);, a=sc.nextInt();, b=sc.nextInt();, c=sc.nextInt();, if(a<b+c && b<a+c && c<a+b), {, if(a*a<b*b+c*c && b*b<a*a+c*c && c*c<a*a+b*b), System.out.println(“Acute angled triangle”);, else if(a*a>b*b+c*c || b*b>a*a+c*c || c*c>a*a+b*b), System.out.println(“Obtuse angled triangle”);, else, System.out.println(“Right angled triangle”);, }, else, System.out.println(“Cannot form a triangle”);, }, }
Page 12 :
9., , 10., , Write a program to accept a mark obtained by a student in computer science and print the, grades accordingly:, Marks, Grade, Above 90, A, 70 to 89, B, 50 to 69, C, below 50, D, , A cloth showroom has announced the following festival discounts on the purchase of items,, based on the total cost of the items purchased:, Total Cost, Less than `2000, `2001 to `5000, `5001 to `10000, Above `10000, , Discount (in Percentage), 5%, 25%, 35%, 50%, , Write a program to input the total cost and compute and display the amount to be paid by the, customer after availing the discount.