The document outlines the principles and features of Object-Oriented Programming (OOP), including concepts such as abstraction, encapsulation, inheritance, and polymorphism. It also discusses Java language features, the Java Development Kit (JDK), and differences between Java and other programming languages like C and C++. Additionally, it covers various programming constructs in Java, such as classes, objects, methods, constructors, and data types.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views9 pages
Unit1 QB
The document outlines the principles and features of Object-Oriented Programming (OOP), including concepts such as abstraction, encapsulation, inheritance, and polymorphism. It also discusses Java language features, the Java Development Kit (JDK), and differences between Java and other programming languages like C and C++. Additionally, it covers various programming constructs in Java, such as classes, objects, methods, constructors, and data types.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9
CS 3391 – OOPs – Unit 1 – Part A
1 What are the OOP Principles?
The principles of object – oriented programming is class inheritance, interface implementation, abstraction of data and behavior, encapsulation of data and class implementation, polymorphism and virtual methods. 2 What are the four cornerstones of OOP? Abstraction: Can manage complexity through abstraction. Gives the complete overview of a particular task and the details are handled by its derived classes, Example : Car. Encapsulation: Nothing but data hiding, like the variables declared under private of a particular class is accessed only in that class and cannot access in any other the class. Inheritance : Is the process in which one object acquires the properties of another object ie., derived object. Polymorphism : One methods different forms, ie., method overriding and interfaces are the examples of polymorphism. 3 What are the features of object oriented programming? Emphasis is on data rather than procedure. Programs are divided into objects. Data is hidden and cannot be accessed by external functions. Objects may communicate with each other through functions. Following bottom-up approach. 4 What are the features of java Language? The features of Java Language are simple, object oriented, portable, platform independent, secured Robust, Architecture neutral, Dynamic, Interpreted, High Performance, Multithreaded and Distributed. 5 How Java supports platform independency? The meaning of platform independent is that, the java source code can run on all operating systems. A compiler is a program that translates the source code for another program from a programming language into executable code. This executable code may be a sequence of machine instructions that can be executed by the CPU directly, or it may be an intermediate representation that is interpreted by a virtual machine. This intermediate representation in Java is the Java is the Java Byte Code. It is the Bytecode that makes it platform independent. 6 Give the contents of Java Environment (JDK). The java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE),an interpreter/loader (java), a compiler(javac), an archiver (jar), a documentation generation (javadoc) and other tools needed in java development. 7 What is Java Interpreter? It is a Java Virtual Machine. An Interpreter is a program that read in as input a source program, along with data for the program and translates the source program instruction by instruction. For example, the Java Interpreter java translate a class file into code that can be executed natively on the underlying machine. 8 Give any 4 different between C and Java. C Java C is a procedural Language. JAVA is Object Oriented language. C is a compiled language. JAVA is an Interpreted language. C uses the top-down approach. JAVA uses the bottom-up approach. C does not support overloading. JAVA supports Method Overloading. 9 Give any 4 different between C++ and Java. C++ generates object code and the same code Java is interpreted for the most part and hence may not run on different platform platform independent. C++ supports structures, unions, templates, Java does not supports pointers, templates, unions, operator overloading, pointers and pointer operator overloading, structures etc. arithmetic. C++ support destructors, which is automatically Java support automatic garbage collection. It does not invoked when the object is destroyed. support destructors as C++ does. 10 Distinguish between procedure oriented programming (POP) and Object oriented programming. (OOP) POP OOP In POP, Program is divided into small parts called In OOP, program is divided into parts called objects. functions. POP does not have any access specifier. OOP has access specifiers named Public, Private protected, etc. POP does not have any proper way for hiding OOP Provides Data Hiding so provides more security. data so it is less secure. In POP, Overloading is not possible In OOP, overloading is possible in the form of Function Overloading and operator overloading. Example of POP are : C, VB, FORTRAN Pascal Example of OOP are : C++, JAVA, VB.NET, C#.NET 11 What are the data types supported in JAVA? Operator in Java is a symbol that is used to perform operations like +,-,*, / etc. There are many types of operators in Java which are Unary Operator, Arithmetic operator, Shift operator, Relational Operators, Bitwise Operator, Logical operator, Ternary Operator and Assignment Operator. 12 Define abstraction. Abstraction refers to the act of representing the essential features without including the background details of explanations. It reduces the complexity and increases the efficiency. Small programs can be easily upgraded to large programs. Software complexity can easily be managed. 13 What is Polymorphism? Polymorphism is the ability to take more than one form and refers to an operation exhibiting different behavior instances. Object oriented programs use polymorphism to carry out the same operation in a manner customized to the object. It allows a single name/operator to be associated with different operation depending on the type of data passed to it. 14 Define Objects and Classes in Java Class is a collection of data and the function that manipulate the data.The data components of the class are called data fields and the function components of the class are called member functions or methods. The class that contains main function is called main class. Object is an instance of a class. The objects represent real world entity. The objects are used to provide a practical basis for the real world. Objects are used to understand the real world. The object can be declared by specifying the name of the class. 15 Write the syntax for declaration of class and creation of objects? A class is declared using class keyword. A class contains both data and method that operate on that data. Thus, the instance variables and methods are known as class members. When creating an object from a class Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object. class Student { String name; int rollno; int age; } Student std=new Student(); std is instance/object of Student class. new keyword creates an actual physical copy of the object and assign it to the std variable. The new operator dynamically allocates memory for an object. 16 Define Encapsulation (Apr/May 2012) (Apr 2017) The wrapping up of data and functions into a single unit is known as data encapsulation. Here the data is not accessible to the outside the class. The data inside that class is accessible by the function in the same class. It is normally not accessible from the outside of the component. 17 What is Inheritance? What are its types? Inheritance is a mechanism of reusing the properties and extending existing classes without modifying them, thus producing hierarchical relationships between them. Inheritance is a property by which the new classes are created using the old classes. The old classes are referred as base classes and the new classes are referred as derivedclasses. That means the derived classes inherit the properties of base class. extends and implements keywords are used to describe inheritance in Java. Types of inheritance are: Single inheritance, Multi-level inheritance, Hierarchical inheritance, Hybrid inheritance. Syntax : class Subclass-name extends Superclass-name { //methods and fields } 19 Define class[NOV/DEC 2011] Class is a template for a set of objects that share a common structure and a common behavior. Eg :
20 What do you mean by Dynamic Initialization?
Java allows variables to be initialized dynamically using any valid expression at the time the variable is declared. Example: Program that computes the remainder of the division operation: class FindRemainer { public static void main(String arg[]) {int num=5,den=2; int rem=num%den; System.out.println(“Remainder is”+rem); } } Output: Remainder is 1 In the above program there are three variables num, den and rem. num and den are initialized by constants whereas rem is initialized dynamically by the modulo division operation on num and den. 24 What do you mean by Variable? What are the rules for variable declaration? A Variable is a named piece of memory that is used for storing data in javaProgram. The syntax of variable declaration : data_type name_of_variable =[initialization]; Rules followed for variable names ( consist of alphabets, digits, unde 1. A variable name must begin with a letter and must be a sequence of letter or digits. 2. They must not begin with digits. 3. Uppercase and lowercase variables are not the same. 4. It should not be a keyword 21 What are the steps for exection of a java program? A program is written in JAVA, the javac compiles it. The result of the JAVA compiler isthe .class file or the bytecode and not the machine native code (unlike C compiler). The bytecode generated is a non-executable code and needs an interpreter to execute on amachine. This interpreter is the JVM and thus the Bytecode is executed by the JVM. And finally program runs to give the desired output.
22 What do you mean by Bytecode? What is JVM and JIT?
Bytecode is an intermediate form of java programs.We get bytecode after compiling the java program using a compiler called javac. The bytecode is to be executed by java runtime environment which is called as Java Virtual Machine(JVM). The programs that are running on JVM must be compiled into a binary format which is denoted by .class files. The JVM executes .class or .jar files, by either interpreting it or using a just-in-time compiler (JIT). The JIT is used for compiling and not for interpreting the file. It is used in most JVMs today to achieve greater speed.
23 What is difference between Methods and Constructor?
A constructor is a member function of a class that is used to create objects of that class. It has thesame name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 24 What are the different datatypes in java? Data types in Java are of two types: 1. Primitive data types (Intrinsic or built-in types ) :- : The primitive data typesinclude boolean, char, byte, short, int, long, float and double. 2. Non-primitive data types (Derived or Reference Types): The non- primitivedata types include Classes, Interfaces, Strings and Arrays. 25 What is Garbage collection? Objects are dynamically allocated by using the new operator, dynamically allocated objects must be manually released by use of a delete operator. Java takes a different approach; ithandles deallocation automatically this is called garbage collection. When no references to anobject exist, that object is assumed to be no longer needed, and the memory occupied by theobject can be reclaimed. Garbage collection only occurs sporadically (if at all) during the execution of your program. It will not occur simply because one or more objects exist that are nolonger used.
26 What is difference between Methods and Constructor?
A constructor is a member function of a class that is used to create objects of that class. It has thesame name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 27 What is passed by reference? If we pass an object to a method, then it is called pass-by-reference or call-by- reference. Reference to the object is passed. So anymodifications done through the object will affect the actual object 28 What is Constructors in Java? What are its types? A constructor is a special method that is used to initialize an object. The name of the constructor and the name of the class must be the same. A constructor does not have any return type. The constructor invoked whenever an object of its associated class is created. It is called constructor because it creates the values for the data fields of the class. Class Car { String name; String model; Car() //Constructor { name=””; model=””;}} There are two types of Constructor Default Constructor Parameterized constructor 29 What is array? How to declare array and how to allocate the memory to for array? Java array contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array. Array in java is index based, first element of the array is stored at 0 index. data_type array_name []; array_name=new data_type[size]; where array_name represent name of the array, new is a keyword used to allocate the memory for arrays, data_type specifies the data type of array elements and size represents the size of an array. For example:int a=new int[10]; 30 Explain how to declare Two Dimensional array? The two dimensional arrays are the arrays in which elements are stored in rows as well as columns. The two dimensional array can be declared and initialized as follows Syntax: 1. dataType[][] arrayRefVar; (or) 2. dataType [][]arrayRefVar; (or) 3. dataType arrayRefVar[][]; (or) 4. dataType []arrayRefVar[];
For example: int a[][]=new int[3][3];
31 What is method in java? How to define and call the method? Method is a programming construct used for grouping the statement together to build afunction. There are two ways by which the method is handled. 1. Defining a method 2. Calling a method Here is example that helps to understand the concept of method defining and calling. public class methDemo { public static void main(String args[]) {inta=10; int b=20; int c=sum(a,b); System.out.println(“The sum of “+a”+” and “+b+” is=”+c); } public static int sum(int num1,int num2) { int ans=num1+num2; returnans; } } 32 What are public static void main(String args[]) and System.out.println() ? Public keyword is an access specifier. Static allows main() to be called without having toinstantiate a particular instance of class. Void does not return any value. Main() is the method where java application begins.String args[] receives any command line arguments during runtime.System is a predefined class that provides access to the system. Out is output stream connected to console.println displays the output. 33 What is instance of operator? Instance of operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceof operator is written as: ( Object reference variable ) instanceof (class/interface type) Eg : boolean result = name instanceof String; 34 What are types of Constructors? Default Constructor, Parameterized Constructor, Copy Constructors 35 What's the difference between an interface and an abstract class? An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class. 36 Explain about Static? When a member is declared as static it can be accessed without any objects of its class or without any reference to any object. These are global variables, no copy of these variables can be made. static can also be declared for methods. 37 List any four Java Doc comments. [NOV/DEC 2011] A Javadoc comment is set off from code by standard multi-line comment tags /* and */. The opening tag, however, has an extra asterisk, as in /**.The first paragraph is a description of the method documented. Following the description are a varying number of descriptive tags, signifying: The parameters of the method (@param),What the method returns (@return) and any exceptions the method may throw (@throws) 38 What are the access specifiers/modifiers for classes in Java? Java Access Specifiers (also known as Visibility Specifiers) regulate access to classes, fields and methods in java. These specifiers determine whether a field or method in c lass, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access.There are 4 types of java access modifiers: Private, Default, Protected and Public 39 What is a package? A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. 40 What is static methods in Java? A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an object to a class. Static method can access static data member and can change the value of it. There are two main restrictions for the static method is that the static method cannot usenon static data member or call non-static method directly. 41 What are the control flow statements in java? A programming language uses control statements to control the flow of execution of program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program. Java’s Selection statements: • if • if-else • nested-if • if-else-if • switch-case • jump – break, continue, return These statements allow you to control the flow of your program’s execution based upon conditions known only during run time. 42 What is static variables in Java? The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class. If you declare any variable as static, it is known static variable. The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc. The static variable gets memory only once in class area at the time of class loading. Advantage - It makes your program memory efficient (i.e it saves memory). UNIT 1 - PART B Questions 1 Explain the method to define classes and objects i)Explain Java Environment with suitable diagrams 2 ii) Explain arrays in java? i. What is class?How do you define a class in java(6) 3 ii. Define Package? How does compiler locate packages ? 7) i. Explain the features of Java and list out the characteristics of JAVA(7) 4 ii. Explain the characteristics of OOPs(6) Summarize the types of constructors supported by JAVA with example.(13) 5 Discuss the usage of constructor with an example using java (dec 2022) 6 With relevant examples describe abstraction and encapsulation. Write a java program 7 that uses an abstraction and encapsulation.(13) Illustrate what is polymorphism? Write a java program that implements 8 Polymorphism.(13) Illustrate with an example the following features of constructors: i. Default constructors(2) ii. Parameterized constructor(2) 9 iii. Overloaded constructors(2) iv. A call to another constructor with this operator(2) v. An object initialization block(2) vi. A static initialization block(3) i. Illustrate OOPS and explain the features of OOPS(7) 10 ii. Demonstrate the static fields and methods used in java(6) i. Distinguish argument and parameter? Discuss with example(7) 11 ii. Differentiate constructor and method with example(6) 12 Difference between OOPS and procedural programming language(13) 13 Assess the different methods in java.Util.Arrays class with example(13) Create a Java program for push and pop operations in stack using 14 Arrays in classes and object.(13) Write a program to perform the following functions using classes, objects, constructors and destructors where essential 15 i. Get as input the marks of 5students in 5 subjects(5) ii. Calculate the total and average(5) iii. Print the formatted result on the screen(5) Make a class Student. The Student class has data members such a roll number, name, 16 branch. Create a class called Exam that has data members roll number and six subject marks. Derive the result class from Student and Exam and has own data members such a total mark, and result. Write a Java program to model the relationships.(15) Create a java program to find a s smallest number in the given array by Creating one 17 dimensional Array and two dimensional array using new operator.(15) i. Write a Java program to Evaluate the following series 1-2+3-4+…+n.(8) 18 ii. Write a Java program to test the prime numbers between the given two limits(7) i. Explain the characteristics of OOPs (6) 19 ii. Explain the features and the characteristics of JAVA. (7) i. What is method? How method is defined? Give example. (6) 20 ii. State the purpose of finalize method in java? With an example explain how finalize method can be used in java program. (7) i) How Java changed the internet ? (9) 21 ii) If semicolons are needed at the end of each statement, why does the comment line not end with a semicolon ? (4) What are the three categories of control statements used in Java ? Explain 22 each category with example. (13) i. Explain this keyword. (6) 23 ii. Write about loops in JAVA.(7) Differentiate between OOP and POP(Dec 2022) 24 25 Appraise the characteristics of object oriented programming languages. (13) 26 Write about the control flow statements used in JAVA 27 Explain the various features of java in detail. (13) 28 What is JVM ? Explain the internal architecture of JVM with neat sketch. (13) 29 Write about Constructor overloading and Constructor Chaining with suitable eg Write a Java program to find biggest of three numbers (Dec 2022) 30 Write a Java program to display the grades of the students by using get() method to get 31 marks and compute() method to compute the average and display () method to display the grade of the student ( Dec 2022) i. Explain the selection statements in Java using suitable examples.(7) 32 ii. Write a Java code using do-while loop that counts down to 1 from10 printing exactly ten lines of “hello’. (6) i. Outline the arithmetic operators in Java. (6) 33 ii. Name the four integer types in Java and outline the bitwise operators that can be applied to the integer types.(7) i. Outline the iteration statements in Java with syntax and example.(9) 34 ii. Outline the use of constructors and this keyword in Java. (4) 35 Write about Java doc Comments briefly