SECTION A (10 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (5 Marks) i. Which of the following is not a valid primitive type : a. char b. double c. int d. data ii. Which of the following is not a valid variable name : a. My_var b. Myvar1 c. My$var d. 1Myvar iii. iv.... is a method of Scanner claas. a. new() b. print() c. println() d. next() In the following class declaration,. is the superclass. a. Class2 b. Class1 c. Class1 and class2 d. None of the above v. In the following diagram,. method illustrates the concept of polymorphism. a. Shape b. Line c. Draw d. Circle 1 (b) Fill in the Blanks: (5 Marks) i. An object, on the other hand, is a unique instance of a filled-in template for which attribute values have been provided, and upon which methods may be performed. ii. iii. iv. Before a variable can be used in a Java program, the type and name of the variable must be declared to the Java compiler. The term instantiation.. is used to refer to the process by which an object is created in memory at run time based upon a class definition. Information Hiding... means that you separate the description of how to use a class from the implementation details, such as how the class methods are defined. v. Inheritance... is a compile-time mechanism in Java that allows you to extend a class (called the base class or superclass) with another class (called the derived class or subclass).
SECTION B (5 x 3 = 15 MARKS) Answer any FIVE Questions. Each Question carries THREE Marks. 2. Following is the code snippet of a Java program: Answer the following questions based on the above code: (i) Write down the names of attributes in the code shown. (ii) Write down the names of methods in the code shown. (iii) Write down the names of object in the code shown. Ans[ 2 ]: Attributes: Mehods: Object: myname,myid myinfo,main firstperson 3. Following is the code snippet of a Java Program: Answer the following questions based on the above code: (i) Write down the names of attributes that are inherited in the ItemInfo class. (ii) Write down the names of methods that are inherited in the ItemInfo class. Ans[ 3 ]: Attributes: Methods: ItemName dispiteminfo 4. Write a code to declare a class using public and private modifiers. Ans[ 4 ]: class test public double att1; private int att2;
5. Differentiate between if statement and switch statement. Ans[ 5 ]: if conditional statement provides the branching based on its condition. A switch statement is similar to an if-else construct in that it allows the conditional execution of one or more lines of code. 6. Write a code to declare and create an array of Objects. Ans[ 6 ]: Class student String stdname; int stdid; class Example public static void main(string args [ ]) Student[ ] students = new Student[10]; for ( i=0;i<10;i++) students[i] = new Student( ); 7. What is the difference between a class and an interface in Java? Ans[ 7 ]: A class defines: The data structure (i.e., the names and types of attributes) of each and every object belonging to that class The operations/methods to be performed by such objects: An interface specifies a set of methods that any class that implements the interface: Contains method headings and constant definitions only. Contains neither instance variables nor any complete method definitions.
SECTION C (3 x 5 = 15 MARKS) Answer any THREE Questions. Each Question carries FIVE Marks. 8. Write a program to implement the concept of method overriding(polymorphism). Ans[ 8 ]: Any program of polymorphism OR class Box public void info() System.out.println("This is a simple box"); class SteelBox extends Box public void info() System.out.println("This is a steel box"); class WoddenBox extends Box public void info() System.out.println("This is a Wodden box"); class LargeWoddenBox extends WoddenBox public void info() System.out.println("This is a Huge Wodden box"); class BoxDemo public static void main(string ary[]) Box b = new Box(); WoddenBox wb = new WoddenBox(); SteelBox sb = new SteelBox(); LargeWoddenBox lwb = new LargeWoddenBox(); b.info(); wb.info(); sb.info(); lwb.info();
9. Write a program that performs following tasks: i. Creates a class named Course havig attributes(coursename, CourseID, CourseLevel). ii. Writes a method named CourseInfo in Course class to display CourseName, CourseID, CourseLevel. iii. Creates two objects of the Course class in another class and assign the values to it as shown below: Course Name Course ID Course Level OOP 213 5 C++ 221 3 Ans[ 9 ]: class Course String coursename; int courseid; int courselevel; void CourseInfo() System.out.prinltln( CourseName is + coursename); System.out.prinltln( CourseID is + courseid); System.out.prinltln( CourseLevel is + courselevel); class Example public static void main (String args[ ]) //*************** Creating Two Courses ******** Course course_1 = new Course (); Course course_2 = new Course (); course_1.courseid=213; course_1.coursename="oop"; course_1.courselevel=5; course_2.courseid=221; course_2.coursename="c++"; course_2.courselevel=3; course1.courseinfo(); course2.courseinfo();
10. Consider the following class declarations: Find out the valid and Not invalid statements in the code shown above.
Ans[ 10 ]:
11. Find out the getter and setter methods in the following class declaration: class Employee private String firstname; private String lastname; private double monthlysalary; public String getfirstname() return firstname; public void setfirstname(string fname) firstname = fname; public String getlastname() return lastname; public void setlastname(string lname) lastname = lname; public double getmonthlysalary() return monthlysalary; public void setmonthlysalary(double m) if(m < 0) monthlysalary =0; else monthlysalary = m; Ans[ 11 ]: Getter Methods: Setter Mehods: getfirstname, getlastname, getmonthlysalary setfirstname, setlastname, setmonthlysalary