[DSE 2123] OBJECT ORIENTED PROGRAMMING - Scheme
Type: MCQ
Q1. How many super classes can be inherited by a subclass? (0.5)
1. **only one
2. zero
3. any number
4. two
Q2. Which of these keywords must be used to monitor for exceptions? (0.5)
1. **try
2. catch
3. finally
4. throw
Q3. Which of these keywords is not a part of exception handling? (0.5)
1. try
2. **thrown
3. throws
4. finally
Q4. What is the keyword used for declaring a constant variable? (0.5)
1. static
2. constant
3. **final
4. const
Q5. What are the rules to define a constructor? (0.5)
1. The constructor should have the same name as the class and have a void as the return
type.
2. The constructor may not have the same name as the class and has no return type even if
void.
3. ** The constructor should have, the same name as the class and have no return type
even if void.
4. The constructor may not have the same name as the class but should have a return type
Q6. What will be the output of the following code?
(0.5)
1. **handled
2. Exception
3. Exceptionhandled
4. No output
Q7. Suppose a and b are two integer variables. How do you test whether exactly one of them is
zero? Identify the correct statement. (0.5)
1. ( a == 0 && b != 0 ) &&( b == 0 && a != 0 )
2. **( a == 0 && b != 0 ) || ( b == 0 && a != 0 )
3. ( a == 0 || b != 0 ) || ( b == 0 || a != 0 )
4. ( a == 0 || b != 0 ) && ( b == 0 && a != 0 )
Q8. What is the minimum number of argument/s that can be passed to “public static void
main(String[] args)”? (0.5)
1. 2
2. **0
3. 1
4. More than 2
Q9. What will be stored in the String sub after executing the following statement ?
String sub = "Welcome".substring(1,5); (0.5)
1. Welc
2. **elco
3. Welco
4. elcom
Q10. What will s1 contain after executing the following lines of Java code?
String s1 = "one";
s1.concat("two");
(0.5)
1. **one
2. two
3. onetwo
4. twoone
Type: DES
Q11. The International Standard Book Number (ISBN) is a unique numeric book identifier, which is
printed on every book. The ISBN is based upon a 10-digit code. The ISBN is valid if:
1 x digit1 + 2 x digit2 + 3 x digit3 + 4 x digit4 + 5 x digit5 + 6 x digit6 + 7 x digit7 + 8 x digit8 + 9 x digit9
+ 10 x digit10 is divisible by 11.
Example: For an ISBN 1401601499:
Sum=1×1 + 2×4 + 3×0 + 4×1 + 5×6 + 6×0 + 7×1 + 8×4 + 9×9 + 10×9 = 253 which is divisible by 11.
Create a class called ISBN and implement the following methods:
• inputISBN( ) to read the ISBN code as a 10-digit number from the keyboard.
• checkISBN() to perform the following check operations :
o If the ISBN is not a 10-digit number, output the message “ISBN should be a 10 digit
number” and terminate the program.
ii) If the number is 10-digit, extract the digits of the number and compute the sum as explained
above. If the sum is divisible by 11, output the message, “Legal ISBN”; otherwise output the
message, “Illegal ISBN”.
Note: Do not use any array. (4)
Solution:
import java.util.Scanner;
class ISBN
{
long isbn;
Scanner S = new Scanner( System.in );
void inputISBN()
{
System.out.println("Enter a 10-digit isbn:");
isbn = S.nextLong();
}
void checkISBN()
{
int digit_count = 0;
long temp = isbn;
while( temp != 0 )
{
digit_count++;
temp /= 10;
}
if( digit_count != 10 )
{
System.out.println("ISBN should be a 10 digit number!");
System.exit(0);
}
temp = isbn;
int sum = 0;
for( int i = 0; temp != 0 ; i++ )
{
sum += ( 10 - i ) * ( temp % 10 );
temp /= 10;
}
System.out.println("sum = "+sum );
if( sum % 11 == 0 )
System.out.println("Legal ISBN");
else
System.out.println("Illegal ISBN");
}
}
class ISBN_Test
{
public static void main( String []args )
{
ISBN ISBN_Obj = new ISBN();
ISBN_Obj.inputISBN();
ISBN_Obj.checkISBN();
}
}
Evaluation Scheme:
Class with inputISBN( ) to read the ISBN code as a 10-digit number from the keyboard→ 1
mark
checkISBN() with correct logic for 10-digit check → 1 mark
correct logic for valid isbn check → 2 marks
Q12. Discuss the significance of an abstract class with the help of a relevant example. Also, explain
the mechanism by which dynamic method dispatch is accomplished. (4)
Solution:
Evaluation Scheme:
Abstract class significance with example –> 2 marks
Dynamic method dispatch explanation → 0.5 mark
Example of dynamic method dispatch – two classes, object reference → 1.5 marks
Q13. Create a class, namely, Array_2D that contains a field for storing a 2D-array of integers. Define
a static method to reverse each element in the 2D-array and stores the result in a separate 2D-array
passed as argument to the static method. Create another class with a main method for creating an
instance of the Array_2D class. Write a complete java program.
Input Matrix: Output Matrix:
. (3)
Solution:
class Array_2D
{
private static int[][] data;
public Array_2D(int[][] data)
{
this.data = data;
}
public static void reverse2DArray( Array_2D ArrayObj , int [][] reversedArray )
{
int[][] inputArray = ArrayObj.data;
int rows = inputArray.length;
int cols = inputArray[0].length;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
reversedArray[i][j] = reverseNumber(inputArray[i][j]);
}
}
}
private static int reverseNumber(int num)
{
int reversedNum = 0;
while (num != 0)
{
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return reversedNum;
}
}
public class Array_2D_Demo
{
public static void main(String[] args)
{
int[][] originalArray = { {123, 456, 789}, {987, 654, 321}, {111, 222, 333} };
Array_2D array2D_obj = new Array_2D( originalArray );
int[][] reversedArray = new int[originalArray.length][originalArray[0].length];
Array_2D.reverse2DArray( array2D_obj , reversedArray );
System.out.println("Original 2D Array:");
printArray(originalArray);
System.out.println("Reversed 2D Array:");
printArray(reversedArray);
}
private static void printArray(int[][] array)
{
for( int i = 0 ; i < array.length ; i ++ )
{
for (int j = 0 ; j < array[i].length ; j++ )
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Evaluation Scheme:
Static method with correct syntax → 1 mark
Correct logic for the reversing and storing in the matrix → 1 mark
Main() with proper method call statement → 1 mark
Q14. Discuss two approaches for implementing variable length arguments in Java. Explain with a
suitable example for each . (3)
Solution:
Evaluation Scheme:
Use array to store arguments with example → 1.5 Marks
Varargs with 3 dots( …) + Example → 1.5 Marks
Q15. Create a class called Employee (instance fields: empID, name, age, Salary) with necessary
constructor and methods corresponding to the Employee instances used in the EmployeeClassDemo
as shown in the Fig 1:
Fig 1: EmployeeClassDemo and Output screen for 15th question
(3)
Solution:
class Employee
{
private int ID;
private String name;
private int age;
private double salary;
Employee( int id , String n, int a , double s )
{
ID = id; name = n; age = a; salary = s;
}
void raiseSalary( double byPercent )
{
double raise = salary * byPercent / 100;
salary += raise;
}
public String toString()
{
return "\n Emp id: "+ID+"\n Name: " + name + "\n Age : "+ age + "\n salary : " + salary;
}
}
public class EmployeeClassDemo {
public static void main(String[] args) {
Employee[] staff = { new Employee( 1, "Anil", 25, 50000 ),
new Employee( 2, "John", 35, 60000 ),
new Employee( 3, "Vinod", 38, 40000 )
};
for( int i = 0 ; i < staff.length; i++ )
staff[i].raiseSalary( 5 ); // Raise everyone's salary by 5%
for( int i = 0 ; i < staff.length; i++ )
System.out.println( staff[i] );
}
}
Evaluation Scheme:
Constructor → 1 mark
raiseSalary() → 1 mark
overridden toString() → 1 mark
Q16. Briefly explain any three methods of String class with syntax and example . (3)
Solution:
Evaluation Scheme:
Three methods syntax with explanation and example → 1 mark for each method
Q17. Given a list of filenames stored in an array of strings. Check whether all the filenames have the
same extension or not. Otherwise, throw a user-defined exception, namely, Invalid_File_Extn. Define
a custom exception handler to display the error message. (3)
Solution:
import java.util.Scanner;
class Invalid_File_Extn extends Exception
{
String des;
Invalid_File_Extn( String des )
{
this.des = des;
}
}
class FilenameExc
{
public static void main(String[] args)
{
Scanner sc = new Scanner( System.in );
System.out.println("Enter how many filenames: ");
int n = sc.nextInt();
String filenames[] = new String[n];
System.out.println("Enter filenames: ");
for(int i=0;i<n;i++)
filenames[i] = sc.next();
int index = filenames[0].lastIndexOf('.');
String ext = filenames[0].substring( index );
try
{
for( int i = 1 ; i < filenames.length; i++ )
{
if( !filenames[i].endsWith(ext) )
throw new Invalid_File_Extn("invalid extension!");
}
System.out.println("All filenames have same extension.. ");
}
catch( Invalid_File_Extn ob )
{
System.out.println(ob.des);
}
}
}
Evaluation Scheme:
Invalid_File_Extn class with a constructor → 1 mark
Correct Logic for extension check → 1 mark
throw statement to generate invalid file extension & catch block → 1 mark
Q18. What will be contents of arr1 and arr2 after executing the following statements. Justify your
answer.
int arr1[]={ 17 , -15 , 27 } , arr2[]={ 65, 41, -21 };
arr1 = arr2; arr2[1] += 7; . (2)
Solution:
Arr1: {65 , 48 , -21 } → 0.5 mark
Arr1: {65 , 48 , -21 } → 0.5 mark
Justification: In java , array is of reference type. So, both arr1 and arr2 point to the same reference.
→ 1 mark