You are on page 1of 9

Type: MCQ

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.

Q4. Which line in this code generates the compilation error?


abstract class X
{ public X() { methodX();}
static{ methodX(); }
abstarct void methodX();} (0.5)

1. Error at line 3
2. Error at line 4
3. ** Error at line 3 and 4
4. None of the mentioned.

Q5. Output of the following code snippet is:


class test extends Exception{
public static void main(String[] args) {
try{ throw new test();
}catch(test t){ System.out.print("Got the test exception");}
finally{System.out.print("Inside finally block");}}} (0.5)

1. Compilation error
2. Run time error
3. **Got the test exception Inside finally block
4. Got the test exception

Q6. Output of the following code snippet is:


class test {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("TestMe");
StringBuffer s2 = new StringBuffer("TestMe");
boolean t = s==s2;
System.out.println( t + " "+ s+45);
}} (0.5)
1. **false TestMe45
2. true TestMe45
3. Compilation error
4. None of the Mentioned
Q7. Write the output for the following code snippet

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 {

public static void main(String[] args) {


System.out.print(new Der(3));

}} (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.

Q12. Compare interface and abstract classes. (2)


1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract
methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only
static and final variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the
implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to
declare interface.
6) An abstract class can extend another Java class and implement multiple Java interfaces. An
interface can extend another Java interface only.
7) An abstract class can be extended using keyword "extends". An interface can be implemented
using keyword "implements".
8) A Java abstract class can have class members like private, protected, etc.Members of a Java
interface are public by default.
Any four Points out of these 0.5 Marks for each point written clearly separately.
Q13. Create an abstract class called “sports” with methods getNumberOfGoals and dispTeam. Derive
classes Hockey and football from “sports”. Write the appropriate data members and said methods to
count the number of goals scored by each team, Display the winning team details by considering two
teams in each case. (3)
Creating abstract class “Sports” with getNumberOfGoals and dispTeam – 0.5M
Deriving Hockey and football from “Sports” + implementation of both methods: logic – 1.5M
Main method with taking input + displaying winning team in each case – 1M

Assumptions: 1. For FootBall team Names: F1 and F2


2. For Hockey team Names: H1 and H2
3. Goals for FootBall Teams F1 and F2 and Hockey Teams H1 and H2 are assigned as
sample values, input can also be taken from keyboard.

import java.util.Scanner;
abstract class Sports2
{ abstract void getNumberOfGoals();
abstract void dispTeam(); }

class FootBall3 extends Sports2


{ String Name; static int fgoal1=0; static int fgoal2=0;
FootBall3(String TeamName)
{Name=TeamName; }

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{

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){

// for (int i = 0; i < s.length(); ++i) {

// int val = s.charAt(i);

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 = "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);
}
}}

You might also like