0% found this document useful (0 votes)
75 views

Solution For Java Sample MID - 1

The document contains a model question paper for a Java programming exam with 4 questions. Each question has multiple subparts asking students to explain concepts, predict output, complete code snippets, or find errors in code. The questions cover topics like object-oriented programming concepts, constructors, inheritance, polymorphism, and more.

Uploaded by

Lury Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Solution For Java Sample MID - 1

The document contains a model question paper for a Java programming exam with 4 questions. Each question has multiple subparts asking students to explain concepts, predict output, complete code snippets, or find errors in code. The questions cover topics like object-oriented programming concepts, constructors, inheritance, polymorphism, and more.

Uploaded by

Lury Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MALLA REDDY UNIVERSITY

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

Hall Ticket No.

NOTE: Answer any Four Questions. All Questions Carry Equal Marks
4*10=40M
a) Explain the basic concepts of object-oriented programming (4M)

b) Predict the output of following Java program (3M)


class T {
int t = 20;
T() { t = 40; } 1.b)Ans. 40 1.c)Ans
} public class one {
1) class Main { public static void main(String arg[]){
public static void main(String args[]) { int x=3,y=5,z=10;
T t1 = new T(); System.out.println(++z+y-y+z+x++);
}
System.out.println(t1.t); }
} }
c) Write the java program to evaluate the following expression to find the value of a , if x=3,
y=5, and z=10: a = ++z + y - y + z + x++; (3M)

a) Explain the types of constructors with a suitable java program. (4M)

b) What will be the output of the following program? (3M)


public class MyFirst {
public static void main(String[] args) {
MyFirst obj = new MyFirst(n);
}
static int a = 10;
2) static int n;
int b = 5;
int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
Model Question Paper

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)

b) This program is related to the generation of Fibonacci numbers. Re-arrange the


scrambled code. For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is
4) the 8th Fibonacci number. (5M)

import java.util.Scanner; //This package for reading input


public class Fibonacci {
public static void main(String args[]) {
int n=s.nextInt(); //Read an integer
Model Question Paper

Scanner sc = new Scanner();


System.out.println(fib(n)); //Generate and print the n-th Fibonacci number
}
static int fib(int n) { import java.util.Scanner;
public class fibonacci {
if (n==1) //Terminal condition public static void main(String args[]) {
return 1; Scanner s=new Scanner(System.in);
int n=s.nextInt(); //Read an integer
else if(n==2) System.out.println(fib(n));
return 0; }
return fib(n - 1) + fib(n - 2); //Recursive call of function static int fib(int n) {
if (n==1)//Terminal condition
} return 0;
} else if(n==2)
return 1;
return fib(n - 2) + fib(n - 1);
}
}
a) Explain types of inheritance in java with example. (5M)

b) Write a java program to print the following output: (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

System.out.println("This is large ");


super.Print();
}
}
class Question43 implements ExtraLarge{
public static void main(String[] args) {
Small s = new Small();
s.Print();
Question43 q = new Question43();
q.display();
}
public void display(){
System.out.println(extra);
}
}
a) Write is difference between Abstract class and Interfaces. (4M)

b) Re-write the below java program which is intended to print 'NPTEL JAVA'. (3M)

public class Question215{


public static void main(String[] args) {

//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

You might also like