Solution For Java Sample MID - 1
Solution For Java Sample MID - 1
School of Engineering
Department of Computer Science Engineering
Year - II Semester MID-I Examination
Model Question Paper
Subject: Programming in Java Code: MR20-1CS0119
Date: 17.04.2023 Duration: 2 Hours Max. Marks: 40
NOTE: Answer any Four Questions. All Questions Carry Equal Marks
4*10=40M
a) Explain the basic concepts of object-oriented programming (4M)
n = 20; }
// Static Block
static Ans. 60, 30, 0, 20, 0
{ a = 60; }
}
c) What will be the error of the following code? Re-write the correct code. (3M)
abstract class MyFirstClass abstract class MyFirstClass
{ {
abstract num (int a, int b) { } abstract num(){ }
}
Type your text
}
a) Explain the difference between “this” and “super” keywords with suitable example. (5M)
b) Complete the code segment to call the method print() of class School first and then call
print() method of class Student. (5M)
//This is the class named School
class School {
// This is a method in class School
public void print() {
System.out.println("Hi! I class SCHOOL.");
}
}
// This is the class named Student
class Student {
3) // This is a method in class Student
public void print() {
System.out.println("Hi! I am class STUDENT");
}
}
public class Question21{
public static void main(String args[]){
//School sch=new
Creating School();
object of class Student
sch.print();
//Student
Creatingstu=new
object of class School
Student();
//stu.print();
Call 'print()' method of class School
// Call 'print()' method of class Student
}
}
a )Explain different types of polymorphism with suitable example. (5M)
-----------------OUTPUT-------------------
This is small
This is medium
This is large
This is extra-large
-------------------------------------------------
However, the code is intentionally modified. Make sure program get correct output
successfully. interface ExtraLarge{
static String extra = "This is extra-large";
void display();
}
interface ExtraLarge{ class Large {
public void Print() {
5) static String extra = "This is extra-large"; System.out.println("This is medium ");
void display(); }
}
} class Medium extends Large {
public void Print() {
class Large { System.out.println("This is small ");
public void Print() { super.Print();
System.out.println("This is medium "); }
} }
class Small extends Medium {
} public void Print() {
super.Print();
System.out.println("This is large ");
class Medium extends Large { }
public void Print() { }
class Question43 implements ExtraLarge{
System.out.println("This is small "); public static void main(String[] args) {
super.Print(); Small s = new Small();
s.Print();
} Question43 q = new Question43();
} q.display();
}
class Small extends Medium { public void display(){
public void Print() { System.out.println(extra);
}
}
Model Question Paper
b) Re-write the below java program which is intended to print 'NPTEL JAVA'. (3M)
//Declare variable with name 'nptel', 'space' and 'java' and proper datatype
String npl, space, java; public class Question215{
public static void main(String[] args) {
//Initialize the variables with proper input //Declare variable with name 'nptel',
//'space' and 'java' and proper datatype
nptel = "NPTEL"; String nptel, space, java;
space = " "; //Initialize the variables with proper input
jva = "JAVA"; nptel = "NPTEL";
6) System.out.print(nptel+space+java+space+nptel); space = " ";
java = "JAVA";
} System.out.print(nptel+space+java);
} }
}
c) Find out what type of errors will occur and explain in detail. (3M)
import java.util.Scanner;
public class CommandLA { error : Incompatible type
public static void main(String[] args) {
int a,b,c; explanation : By default the command line
a= (args[0]); arguments are strings, here we are
trying to convert string type to integer
b= (args[1]); type implicitly but it can't be possible
c=a+b;
System.out.println(c);
}}
6. 1)Ans
5.1)Types of inheritance
a.Single Inheritance.
b.Multiple Inheritance.
c.Multilevel Inheritance.
d.Hierarchical Inheritance.
e.Hybrid Inheritance.
Note : But in here we can achive only the first three inheritance by using classes,the remaining two can be achived by
using interfaces
3.a) This :
this keyword is used to invoke the current instance of the class
Example :
public class super_example{
public static void main(String [ ] args){
one o1=new one(20);
o1.show();
}
}
class one{
int a=10;
one(int a){
this.a=a;
}
void show(){
System.out.println("value of a is "+a);
}
}
Super :
super keyword is used to invoke the super class method in the subclass.
Note : here we can consider the "5.b" question is example for the super keyword
4.a) Types of polymorphism :
1.Run-time polymorphism(overloading)
2.Compile-time polymorphism (overriding)
1. Run-time polymorphism :
re-using the same function using the super keyword
example :
here we can consider the "5.b" question is example for the super keyword
2. Compile-time polymorphism:
creating the different methods using same name but different parameters .
exapmle :
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
2.a)
In Java, constructors can be divided into 3 types:
1.No-Arg Constructor
2.Parameterized Constructor
3.Default Constructor
1.No arguments:
creating the constructor without the parameters in it.
example :
class main {
main(){
System.out.print("hello world");
}
public static viod main (string[ ] args){
main m1=new main();
}
}
2.Parameterized Constructor :
creating the constructor with parameters
example :
class main {
main(int a,int b){
System.out.print("sum of a and b is " + a+b);
}
public static void main (String [ ] args ){
main m1=new main (10 ,20);
}
}
3.Default constructor :
In the program if we don't create the constructor then it by default creates the constructor.
example :
class Main {
int a;
boolean b;
public static void main(String[] args) {
Main obj = new Main();
System.out.println("Default Value:");
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
1.a) The basic concepts of object-oriented programing are these
1.Object
2.Class
3.Inheritance
4.Polymorphism
5.Abstraction
5.Encapsulation