1 Mark Questions
Question 1 : Identify the statements listed below as assignment, increment, method invocation or object creation statements.
i) System.out.println(“Java”)
ii) costPrice=(457.50)
iii)Car hybrid = new Car();
iv) petrolPrice++;
Answer :
i) System.out.println(“Java”); – method invocation statement.
ii) costPrice=(457.50); – assignment statement.
iii)Car hybrid = new Car(); – object creation statement.
iv) petrolPrice++; – increment statement.
Question 2 : What will be the output for the following statement?
System.out.println
(“The king said\”Being at beginning!\” to me.”);
Answer : The output of the following statement is :
The king said “Begin at beginning!” to me.
Question 3 : What is an exception?
Answer : An exception is an abnormal condition that arises in a code sequence at runtime. Error might be because of the following:
- a programming mistake
- bad input
- problem in network connectivity
- problem in resource allocation
Question 4 : Why is an object called an instance of a class?
Answer : Instance variable are variables within a class but outside any method. In other words, instance variable belongs to an instance of a class. Another way of saying that is instance variables belong to an object, since an object is the instance of a class. Instances can be accessed from any method, constructor or block of that particular class.
2 Marks Questions
Question 5 : Name any two wrapper classes.
Answer : Wrapper classes are:
i) Integer ii) Boolean
Question 6 : Write a Java statement to create an object “mp4” of class “digital”.
Answer : Class name is “digital” and object name is “mp4”
digital mp4 = new digital();
Question 7 : What does a class encapsulate?
Answer : Class encapsulation is facility to hiding an attribute or method from outer world. It is the technique of making the fields in a class private and providing access to those field via public methods.
Question 8: Name the type of errors (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.
iii) Missing semicolon.
Answer : i) runtime error
ii) logical error
iii) syntax error
iv) static keyword
Question 9 : Name the keyword.
Answer : i) throws IOException
ii) static keyword
Question 10 : What is the difference between an object and a class?
Answer : Difference between object and class:
Object | Class |
---|---|
Object is instance of class or variable of class. | Class is mechanism of binding data members and associated methods in a single unit. |
It is physical existence. | It is logical |
Memory space is allocated, when it is created. | Memory space is not allocated, when it is created. |
Question 11 : Why is class called a factory of objects?
Answer : Class is called a factory of objects, because the class contains a collection of objects and all the attributes to create an object.
Question 12 : Why is class known as composite data type?
Answer : A class is known as composite data type because it binds both member variables and functions as a single identity. Composite data type hold reference of any variable, it is assigned with the help of reference e.g. a class abc have two value x and y and the value of these variables cannot be assigned directly. They can be assigned with the help of reference.
abc obj1; //Creating an object of abc class obj1.x = 5; //Referencing the variable obj1.y = 10;
Question 13 : Mention any two attributes required for class declaration.
Answer : Two attributes required for class declaration:
i) Modifiers such as public, private.
ii) The class name, with the initial letter capitalised by convention.
Question 14 : What does the following mean?
Employee staff = new Employee();
Answer: In this statement, Employee is class and it creates an object whose name is staff.
Question 15 : Write a Java statement to input/read the following from the user using the keyboard.
i)Character
ii)String
Answer : i) char c=(char)object.read();
ii) String s=object.readLine();
Question 16 : What is the use of exception handling in java?
Answer : Exception handling is a term associated with handling
Question 17 : Explain the term object using an example.
Answer :
Question 18 : What is a wrapper class? Give an example.
Answer : Wrapper class is used to convert any primitive data type into object. A wrapper class also contains a number of methods, which may be used in the processing of the corresponding data type. e.g. the wrapper class for int data type is integer.
15 Marks Questions
Question 19 : Write a class with name employee and basic as its data member, to find the gross pay of an employee for the following allowances and deduction. Use meaningful variables.
Dearness Allowance = 25% of Basic Pay
House Rent Allowance = 15% of Basic Pay
Provident Fund = 8.33% of Basic Pay
Net Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Gross Pay = Net Pay – Provident Fund
Answer :
class employee { private int Basic; void salary(int Basic, String name) //Enter basic salary and name { double DA, HR, PF, NP, GP; DA = (20.5/100)*Basic; HR=(15.0/100)*Basic; PF=(83../100)*Basic; NP=Basic+DA+HR; GP = NP-PF; System.out.println("Gross Pay"+GP); } }