Professional Documents
Culture Documents
Q1. Which of the following statements is true about exception handling in Java? (0.5)
1. ** A try block can have many catch blocks but only one finally block
2. A try block must have at least one catch block to have a finally block
3. A try block can have many catch blocks and many finally blocks
4. A try block must have one finally block for each catch block
Q2. Which of these is a mechanism for naming and visibility control of a class and its content? (0.5)
1. **Packages.
2. Object.
3. Interfaces.
4. None of the mentined
Q3. Which of this access specifies can be used for a class so that its members can be accessed by a
different class in the same package? (0.5)
1. public
2. No modifier
3. Protected
4. **All of the mentioned.
1. Error at line 3
2. Error at line 4
3. ** Error at line 3 and 4
4. None of the mentioned.
1. Compilation error
2. Run time error
3. **Got the test exception Inside finally block
4. Got the test exception
class first
{
String a=" ";
first(){ disp(); };
void disp() { a= a+" CPU "; }
}
class second extends first
{
second(){ disp();}
void disp() { a= a+" RAM"; }
}
class Q1
{
public static void main(String args[]){
second s = new second();
System.out.println(s.a);
}}(0.5)
1. CPUCPU
2. **RAMRAM
3. RAMCPU
4. CPURAM
Q8. Write the output for the following code snippet
abstract class first
{
abstract void add(int a, int b);
abstract void sub(int a, int b);
}
class second extends first
{ void add(int a, int b)
{
int c;
c = a++;
c = c+b;
System.out.println(c); }}
class Q2
{
public static void main(String args[]){
second s = new second();
s.add(5,10);
}}(0.5)
1. 15
2. **Compilation error.
3. 16.
4. 10
Q9. Write the output for the following code snippet
class first
{
int a=0;
first(){ add(); };
void add() { a= a+30; }
}
class second extends first
{
second(){ add();};
void add() { a= a+40; }
}
class Q3
{
public static void main(String args[]){
second s = new second();
s.add();
System.out.println(s.a);
}
}(0.5)
1. 30
2. **120
3. 110
4. 40
Q10. Write the output for the following code snippet
class Base{
int b;
Base(int b){this.b=b;}
}
class Der extends Base{
int b;
Der(int b){super(10);
this.b=b;}
public String toString() {
return b+ " "+super.b+" ";
}
}
public class Grand {
}} (0.5)
1. **3 10
2. 10 3
3. 13
4. None of the mentioned
Type: DES
Q11. What is the output of the following code? Justify the answer.
interface a
{ void meth1(); }
interface b
{ void meth2(); }
class c implements a,b{
public void meth1(){
System.out.println("Meth1"); }
}
class interfacequiz{
public static void main(String args[])
{ c o1=new c();
o1.meth1();
o1.meth2(); } }. (2)
Compilation error
Class c has to implement 2 methods but contains only one so it should be declared as abstract or
definition for second method should be given.
import java.util.Scanner;
abstract class Sports2
{ abstract void getNumberOfGoals();
abstract void dispTeam(); }
void getNumberOfGoals()
{ if (Name.equals("F1"))
fgoal1 = 5;
else fgoal2 =6;}
void dispTeam()
{ if (fgoal1 > fgoal2)
System.out.println("Winning team is F1 with "+ fgoal1+" goals");
else
System.out.println("Winning team is F2 with "+ fgoal2 + " goals");}}
class Hockey2 extends Sports2
{ String HName; static int hgoal1=0; static int hgoal2=0;
Hockey2(String TeamName)
{HName=TeamName; }
void getNumberOfGoals()
{ if (HName.equals("H1"))
hgoal1=8;
else hgoal2=7;}
void dispTeam()
{ if (hgoal1 > hgoal2)
System.out.println("Winning team is H1 with "+hgoal1+" goals");
else
System.out.println("Winning team is H2 with "+ hgoal2 + " goals"); }}
class SportDemo3
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
String sp;
System.out.println("enter Hockey or FootBall?");
sp = sc.next();
if (sp.equals("FootBall"))
{FootBall3 f3 = new FootBall3("F1");
FootBall3 f4 = new FootBall3("F2");
f3.getNumberOfGoals();
f4.getNumberOfGoals();
f3.dispTeam();
}
else
{Hockey2 h2 = new Hockey2("H1");
Hockey2 h3 = new Hockey2("H2");}
h2.getNumberOfGoals();
h3.getNumberOfGoals();
h2.dispTeam();}}
Q14. Write a java program with a method that takes a String and that returns a new String encoded
using encoding technique. The encoding is done by taking each letter in a string and adding an
integer to it. For example, if integer value is 13 then 'a' becomes 'n' and 'b' becomes 'o'. The letters
wrap around at the end, so 'z' becomes 'm'.
Assume that the String contains only lower case letters and spaces. Do not encode the spaces.
If the String contains any character other than lower case letters & space , then throw a user defined
exception”InvalidContent” stating “ There is a character other than a lower case letter & space” . If
exception does not occur then display the encrypted string.
Example:
If the String is “Best of Luck !!!” , then the exception should be displayed & thrown as there is an
upper case letter , and special character in the String.
If the String is “ best of luck” , then the words should be encrypted and displayed as follows: orfg bs
xhpy”. (3)
i. User Defined class “Invalid Content”. 0.5 marks
ii. Class definition with suitable constructors & encoding method. 1.5 mark
iii. Exception handling . 1 mark
class InvalidContent extends Exception{
k=13;
if( val != 32){
if( val +k >96){
k = dup;
System.out.println(newS);
}
else{
throw new InvalidContent();
}
}
}
// Driver Code
public static void main(String[] args) {
String str = "abcd Efgh";
int k = 13;
// function call
try{
encode(str, k);
}
catch(InvalidContent e)
{
System.out.println(e);
}
}
}
class InvalidContent extends Exception
{
public String toString()
{
return " InvalidContent";
}
}
public class GFG {
static void encode(String s, int k) throws InvalidContent {
String newS = "";
for (int i = 0; i < s.length(); ++i)
{
int val = s.charAt(i);
if (val>=97 && val<=122 || val == 32)
{
int dup = 13;
k=13;
if( val != 32)
{
if( val +k >96)
{
if (val + k > 122 )
{
k -= (122 - val);
newS += (char)(96 + k);
// k = k % 26;
} else
{
newS += (char)(val + k);
System.out.println(val);
}
}
}
k = dup;
System.out.println(newS);
}
else{
throw new InvalidContent();
}
}
}
// Driver Code
public static void main(String[] args)
{
String str = "aaaaaaaaaa";
int k = 13;
// function call
try
{
encode(str, k);
}
catch(InvalidContent e)
{
System.out.println(e);
}
}}