Topics in computer science (java), Exams of Java Programming

Solved Past paper of java programming

Typology: Exams

2020/2021

Available from 01/18/2022

AwaisArain
AwaisArain 🇵🇰

5 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
12/9/2020
Solved Past
Papers Of JAVA
2020-2014
Mahnoor Shoukat
Instructor Ma’am Rida
BCS 6th Semester
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Topics in computer science (java) and more Exams Java Programming in PDF only on Docsity!

12/9/2020 Solved Past

Papers Of JAVA

Mahnoor Shoukat

Instructor Ma’am Rida BCS 6th Semester

Question 1:

a. The output is : num1 != num num3 ==num b. The output is: 1 2 c. There is no output. Error will appear in line 4.

Question 2:

a. Dynamic Method Dispatch:

Dynamic method dispatch is the mechanism by which a call to an overridden function is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism For example: class A { void callme() { System.out.println("Inside A's callme method"); } } class B extends A { void callme() { System.out.println("Inside B's callme method"); } } class Dispatch { public static void main(String args[]) { A a = new A(); // object of type A B b = new B(); // object of type B A r; // obtain a reference of type A r = a; // r refers to an A object r.callme(); // calls A's version of callme r = b; // r refers to a B object r.callme(); // calls B's version of callme } }

b. Encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.

Polymorphism

Polymorphism (from the Greek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.

c. Java

Java is an Objected-Oriented Programming Language developed by James Gosling at Sun Microsystems (now acquired by the Oracle Corporation). In 1995. Java is platform independent language. JVM & its purpose and operations: Byte code is a highly optimize set of instructions design to be executed by Java run time system which is called Java Virtual Machine (JVM). JVM is an interpreter for byte code. Java Virtual Machine is used as an abstraction between the OS and the Java programs.

Question 3:

a.

Sum of first 100 prime numbers: import java.util.*; public class sum { public static void main(String[] args) { int sum = 1; int ctr = 0; int n = 0; while (ctr < 100) { n++; if (n % 2 != 0) { // check if the number is even if (is_Prime(n)) { sum += n; } } ctr++; } System.out.println("\nSum of the prime numbers till 100: "+sum); }

Distributed

Java is designed for the distributed environment of the internet.

Dynamic

Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry an extensive amount of run-time information that can be used to verify and resolve accesses to objects at run-time.

Difference between Abstract class and Interface

Question 4: Not done yet

Question 5:

a. Factorial Program

class Factorial{ public static void main(String[] args){ int n, cn, fact; Scanner sc= new Scanner(System.in); System.out.print(“Enter number”); n=sc.nextInt(); System.out.println(“Factorial of number is” + n); for(int i =1; i<=n; i++){ cn=i; fact=1; for(int j=1;j<=cn:j++){

fact =fact*j; } System.out.print(cn,fact); } } }

b. There is no output, an error will appear in line 5

The program for mean and variance class MeanAndVariance { public static void main(String args[]) { double[] input={5,10,15,20,25}; double n=5,sum=0,mean; for(int i=0;i<n;i++) { sum=sum+input[i]; } mean=sum/n; System.out.println("Mean :"+mean); sum=0; for(int i=0;i<n;i++) { sum+=Math.pow((input[i]-mean),2); } mean=sum/(n-1); double deviation=Math.sqrt(mean); System.out.println("Variance :"+deviation); } }

c. Not done yet

Question 6: Not done yet

Question 2:

a.

The following are the important differences between print() and println(). Sr. No. Key print() println() 1 Implementation print method is implemented as it prints the text on the console and the cursor remains at the end of the text at the console. On the other hand, println method is implemented as prints the text on the console and the cursor remains at the start of the next line at the console and the next printing takes place from next line. 2 Nature The prints method simply print text on the console and does not add any new line. While println adds new line after print text on console. 3 Arguments print method works only with input parameter passed otherwise in case no argument is passed it throws syntax exception. println method works both with and without parameter and do not throw any type of exception.

b. Symbolic Constants

Symbolic constants in Java are named constants. We use the final keyword so they cannot be reassigned a new value after being declared as final. They are symbolic because they are named. Here are a couple examples of symbolic constant variables. final int MAX_THREADS = 20; final int MAX_USERS = 40; final int MAX_SESSIONS = 50;

c. Not done yet
d. Not done yet

Question 3: Not done yet

Question 4:

a. Not done yet
b. Abstract Class

 There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.  That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.  Such a class determines the nature of the methods that the subclasses must implement.  Java provide abstract method to override all the necessary methods.  Abstract classes cannot be used to instantiate objects, they can be used to create object references.  It must be possible to create a reference to an abstract class so that it can be used to point to a subclass object.

Example

// A Simple demonstration of abstract. abstract class A { abstract void callme(); // concrete methods are still allowed in abstract classes void callmetoo() { System.out.println("This is a concrete method."); } } class B extends A { void callme() { System.out.println("B's implementation of callme."); } } class AbstractDemo { public static void main(String args[]) { B b = new B(); b.callme(); b.callmetoo(); } }

Limitation

This is another form of inheritance where the subclass restricts the inherited behavior. It does not hold the principle of substitutability. Example: defining Queue as a subclass of Dequeue.

Combination

This is another form of inheritance where the subclass inherits properties from multiple parent classes. Java does not support multiple inheritance type.

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. In java programming, multiple and hybrid inheritance is supported through interface only.

 Single Inheritance : When a class inherits another class, it is known as a single
inheritance. In the example given below, Dog class inherits the Animal class, so there is

the single inheritance. class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class TestInheritance{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); }}

 Multilevel Inheritance: When there is a chain of inheritance, it is known as multilevel
inheritance. As you can see in the example given below, BabyDog class inherits the Dog

class which again inherits the Animal class, so there is a multilevel inheritance. Multiple Inheritance is not supported in java. class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class BabyDog extends Dog{ void weep(){System.out.println("weeping...");} } class TestInheritance2{

public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }}  Hybrid Inheritance: Any combination of above three inheritance (single, hierarchical and multi level) is called as hybrid inheritance.  Multipath Inheritance: Multiple inheritance is a method of inheritance in which one derived class can inherit properties of base class in different paths. This inheritance is not supported in .NET Languages such as C#.  Hierarchical Inheritance: When two or more classes inherits a single class, it is known

as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the

Animal class, so there is hierarchical inheritance. class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class Cat extends Animal{ void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }} Q) Why multiple inheritance is not supported in java? (Extra Question) To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error. class A{ void msg(){System.out.println("Hello");} }

Question 7:

a. Not done yet.
b. Difference Between Multitasking and Multithreading

Multitasking Multithreading In multitasking, several programs are executed concurrently e.g. Java compiler and a Java IDE like NetBeans or Eclipse. In multi-threading multiple threads execute either same or different part of program multiple times at the same time. In multi-tasking, CPU switches between multiple programs to complete their execution in real time In multi-threading CPU switches between multiple threads of the same program Process are heavyweight as compared to threads, they require their own address space, which means multi-tasking is heavy compared to multithreading. Inter-process communication is expensive and limited and context switching from one process to another is expensive and limited.

c. Not done yet
d. Not done yet.

b. Break: In Java, the break statement has three uses. First, as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop. Third, it can be used as a "civilized" form of goto. c. Ways to define arrays: i. type[ ] var-name; ii. type var-name[ ]; iii. array-var = new type[size];

iv. array-var[] = { any element or data to be stored in array}
d. Difference between array and link list

Array Linklist An array is the data structure that contains a collection of similar type data elements The Linked list is considered as a non- primitive data structure contains a collection of unordered linked elements known as nodes. In the array the elements belong to indexes, i.e., if you want to get into the fourth element you have to write the variable name with its index or location within the square bracket In a linked list though, you have to start from the head and work your way through until you get to the fourth element Accessing an element in an array is fast Linked list takes linear time, so it is quite a bit slower. Operations like insertion and deletion in arrays consume a lot of time On the other hand, the performance of these operations in Linked lists are fast. Arrays are of fixed size Linked lists are dynamic and flexible and can expand and contract its size. In an array, memory is assigned during compile time In a Linked list it is allocated during execution or runtime. Elements are stored consecutively in arrays Elements are stored randomly in Linked lists. The requirement of memory is less due to actual data being stored within the index in the array There is a need for more memory in Linked Lists due to storage of additional next and previous referencing elements. In addition memory utilization is inefficient in the array. Memory utilization is efficient in the linked list

Question 3:

a. Principles of OOP:

There are 4 major principles that make a language Object Oriented. These are Encapsulation, Data Abstraction, Polymorphism and Inheritance. These are also called as four pillars of Object Oriented Programming.

Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Abstraction: Abstract means a concept or an Idea which is not associated with any particular instance. Polymorphism: Polymorphism (from the Greek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions. Inheritance: Inheritance is the process by which one object acquires the properties of another object.

b. Inheritance

Inheritance is the process by which one object acquires the properties of another object. It allows the creation of hierarchical classifications. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

c. Polymorphism & its Types

It means one name many forms. It is further of two types — static and dynamic. Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding. It is closely related to inheritance. We can write a code that works on the superclass, and it will work with any subclass type as well.

d. Method Overriding

In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods.

Question 4: Not done yet.

Question 5: Not done yet.

Question 6: Not done yet.