OOP 1 IMP Question For SEM 4 CE/IT | GTU Medium

 1 . Explain java buzzwords

Simple - Java is very easy to learn, and its syntax is simple, clean and easy to understand.

Object - Oriented-Java is anobject-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that incorporate both data and behavior.

Portable - Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation.

Platform independent - Java is platform independent because it is different from other languages likeC,C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs

Secured - Java is best known for its security. With Java, we can develop virus-free systems

Robust - It uses strong memory management, there is a lack of pointers that avoids security problems.

Architecture neutral - Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive types is fixed.

High Performance - Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.

Multithreaded - A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.

Distributed - Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet.

Dynamic - Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.


2 . Explain JDK, JVM and JRE

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode.

JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent because the configuration of eachOSis different from each other. However, Java is platform independent. There are three notions of the JVM:specification,implementation, and instance.

 JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

The implementation of JVM is also actively released by other companies besides Sun Micro Systems.

JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop Java applications andapplets. It physically exists. It contains JRE + development tools.

JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:oStandard Edition Java PlatformoEnterprise Edition Java PlatformoMicro Edition Java Platform.

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application

 

 3 . What is the difference between oop and procedural oriented language?

Procedural Programming : 

  • It is a programming language that is derived from structure programming and based upon the concept of calling procedures. It follows a step-by-step approach in order to break down a task into a set of variables and routines via a sequence of instructions.
  • It is less secure than OOPs.
  • It follows a top-down approach.
  • In procedural programming, data moves freely within the system from one function to another.
  • It is structure/procedure-oriented.
  • It gives importance to functions over data
  • In procedural programming, there are no virtual classes.
  • There is not any proper way for data hiding.
  • In Procedural programming, a program is divided into small programs that are referred to as functions.
  • Examples of Procedural programming include C, Fortran, Pascal, and VB.

 

4 . Explain class and object with respect to java

OBJECT

Object in Java is the physical as well as a logical entity, that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc.

For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It is used to write, so writing is its behavior.


CLASS

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain: Fields, Methods, Constructors, Blocks, Nested class and interface.

Syntax to declare a class :


class<class_name> 

{ 

field;method; 

}

 

5 . Write a program to add two numbers which take input from user.

import java.util.Scanner; 

public class Main

 {public static void main(String[]args)

 { 

int a,b,sum; 

Scanner sc=newScanner(System.in); 

System.out.println("EnterFirstNumber:");

a=sc.nextInt(); 

System.out.println("EnterSecondNumber:"); 

b=sc.nextInt(); 

sc.close();

 sum=a+b;System.out.println("Sumofthesenumbers:"+sum); 

} 

} 

Output :

Enter First Number: 121

Enter Second Number:19 

Sum of these numbers:140

 

 6 . Explain type-conversion in java

In Java,type casting (Type Conversion)is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer.

There are two types of type casting: 

o Widening Type Casting 

o Narrowing Type Casting

Widening Type Conversion: -Converting a lower data type into a higher one is called widening type casting. 

It is also known asimplicit conversionorcasting down. It is done automatically. It is safe because there is no chance to lose data. 

It takes place when: 

o Both data types must be compatible with each other. 

o The target type must be larger than the source type.

 byte->short->char->int->long->float->double

For example, the conversion between numeric data type to char or Boolean is not done automatically. Also, the char and Boolean data types are not compatible with each other.

Narrowing Type Casting: -Converting a higher data type into a lower one is callednarrowingtype casting. 

It is also known asexplicit conversionorcasting up. It is done manually by the programmer. If we do not perform casting, then the compiler reports a compile-time error.

 double->float->long->int->char->short->byte

Let's see an example of narrowing type casting.

In the following example, we have performed the narrowing type casting two times. First, we have converted the double type into long data type after that long data type is converted into int type.

 

7 . Difference between Nested if and multi-way if statements

if(condition1) 

{ 

If(condition2) { 

Operation 

} 

}

This nested if statement. The second body which is operation is executed only when both conditions are true. If the first condition false, then it doesn't even checks the second condition.

But if you have if else statement

If (condition1) { 

Operation1 

} 

Else if (operation2) { 

Operation2 

}Else { 

Operation3

 }

The if first condition true then executed operation1 but don't execute operation2 and operation3. If first condition fails,the checks second condition if the second condition is true operation2 is executed only. If the second condition fails as well only then the compiler execute operation3

 

8 . Explain use of continue and break with an example.

The break and continue statements are the jump statements that are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression. These statements can be used inside any loops such as for, while, do-while loop. 

Break

Basically, break statements are used in situations when we are not sure about the actual number of iteration for the loop, or we want to terminate the loop based on some condition. 

Syntax : Break;

Example

  // Java program to demonstrate using 

// break to exit a loop

class GFG {public static void main(String[] args){ 

// Initially loop is set to run from 0-9

 for (int i = 0; i < 10; i++) {

// Terminate the loop when i is 5

 if (i == 5)break; 

System.out.println("i: " + i); 

}

 System.out.println("Out of Loop");

} 

} 

 

Output 

i: 0 

i: 1 

i: 2

i: 3

i: 4 

Out of Loop

 

Continue

Basicallycontinue statements are used in the situations when we want to continue the loop but do not want the remaining statement after the continue statement. 

Syntax : continue;

Using continue tocontinue a loop

Using continue, we can skip the current iteration of a loop and jumps to the next iteration of the loop immediately.

Example:

//Java program to demonstrates the continue

//statement to continue a loop

class GFG{public static void main(Stringargs[]){

 for(inti=0;i<10;i++){ 

//If the number is 2 

//skip and continue 

if(i==2)continue;

 System.out.print(i+"");

}

} 

} 

 

Output

 0 1 3 4 5 6 7 8 9

 

9 . Write a difference between call by value and call by reference.

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.

Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.

That is the difference between call by value and call by reference but in java there is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method

Post a Comment

Previous Post Next Post

Contact Form