Arrays and OBJ
Arrays and OBJ
Two Dimensional Arrays (2d array): A two dimensional array represents several
rows and columns of data.
To represent a two dimensional array, we should use two pairs of
square braces [ ] [ ] after the array name.
For example, the marks obtained by a group of students in five different subjects can
be represented by a 2D array
o We can declare a two dimensional array and directly store elements at the time of its
declaration, as:
int marks[] [] = {{50, 60, 55, 67, 70},{62, 65, 70, 70, 81}, {72, 66, 77, 80, 69} };
Three Dimensional arrays (3D arrays): We can consider a three dimensional array
as a combination of several two dimensional arrays.
To represent a three dimensional array, we should use three pairs of square braces [ ]
[ ] [ ] after the array name
o We can declare a three dimensional array and directly store elements at the time of
its
declaration, as:
int arr[ ] [ ] [ ] = {{{50, 51, 52},{60, 61, 62}}, {{70, 71, 72}, {80, 81, 82}}};
Program 2: Write a program to take a 2D array and display its elements in the form
of a matrix
The languages like C++ and Java use classes and object in their programs and are
called Object Oriented Programming languages The main task is divided into several
modules and these are represented as classes Each class can perform some tasks for
which several methods are written in a class This approach is called Object Oriented
approach
1 Main program is divided into small 1 Main program is divided into small
parts depending on the functions object
depending on the problem
2 The Different parts of the program 2 Functions of object linked with object
connect with each other by parameter using
passing & using operating system message passing
3 Every function contains different data 3 Data & functions of each individual
object act
like a single unit
4 Functions get more importance than 4 Data gets more importance than
data in program functions in
Program
5 Most of the functions use global data 5 Each object controls its own data
6 Same data may be transfer from one 6 Data does not possible transfer from
function to another one
object to another
7 There is no perfect way for data hiding 7 Data hiding possible in OOP which
prevent
illegal access of function from outside of
it This
is one of the best advantages of OOP also
8 Functions communicate with other 8 One object link with other using the
functions maintaining as usual rules message
Passing
9 More data or functions can not be 9 More data or functions can be added
added with program if necessary For this with program if necessary. For this
purpose purpose full program need not to be
full program need to be change change
Features of OOP:
Class: In object-oriented programming, a class is a programming language construct
that is used as a blueprint to create objects This blueprint includes attributes and
methods that the created objects all share Usually, a class represents a person, place,
or thing - it is an abstraction of a concept within a computer program.
General form of a class:
class class_name
eg: class Student
Encapsulation: Wrapping up of data (variables) and methods into single unit is called
Encapsulation Class is an example for encapsulation
Encapsulation can be described as a protective barrier that prevents the code and data
being randomly accessed by other code defined outside the class
Encapsulation is the technique of making the fields in a class private and providing
access to the fields via methods If a field is declared private, it cannot be accessed by
anyone outside the class
Abstraction: Providing the essential features without its inner details is called
abstraction
(or) hiding internal implementation is called Abstraction
A class contains lot of data and the user does not need the entire data The advantage
of abstraction is that every user will get his own view of the data according to his
requirements and will not get confused with unnecessary data
For example A bank clerk should see the customer details like account number, name
and balance amount in the account He should not be entitled to see the sensitive data
like the staff salaries, profit or loss of the bank etc So such data can be abstracted
from the clerks view
Inheritance: Acquiring the properties from one class to another class is called
inheritance
(or) producing new class from already existing class is called inheritance
In the above example, the child has inherited its family name from the parent class
just by inheriting the class
Polymorphism: The word polymorphism came from two Greek words „poly‟ means
„many‟ and „morphos‟ means „forms‟ Thus, polymorphism represents the ability to
assume several different forms
The ability to define more than one function with the same name is called
Polymorphism
i. Which of the following option leads to the portability and security of Java? (a)
Bytecode is executed by JVM (b)The applet makes the Java code secure and
portable (c) Use of exception handling (d) Dynamic binding between objects
ii. _____ is used to find and fix bugs in the Java programs. (a) JVM (b) JRE(c)JDK
(d)JDB
iii. What is the return type of the hashCode() method in the Object class? (a) Object
(b) int (c) long (d) void
iv. Which of the following is a valid long literal? (a) ABH8097 (b) L990023
(c)904423 (d) 0xnf029L
v. What does the expression float a = 35 / 0 return? (a) 0 (b) Not a Number (c)
Infinity (d) Run time exception
vi. Evaluate the following Java expression, if x=3, y=5, and z=10: ++z + y - y + z +
x (a) 24 (b) 23 (c) 20 (d)25
vii. Which of the following tool is used to generate API documentation in HTML
format from doc comments in source code? (a) javap tool (b) javaw command (c)
Javadoc tool (d) javah command
viii. In which memory a String is stored, when we create a string using new operator?
(a) Stack (b) String memory(c) Heap memory (d) Random storage space
ix. Which of the following creates a List of 3 visible items and multiple selections
abled (a) new List(false, 3) (b) new List(3, true) (c) new List(true, 3) (d) new
List(3, false)
x. Which method of the Class.class is used to determine the name of a class
represented by the class object as a String? (a) getClass() (b) intern() (c)
getName() (d) toString()
xi. In which process, a local variable has the same name as one of the instance
variables? (a) Serialization (b) Variable Shadowing (c) Abstraction (d) Multi-
threading
xii. Which package contains the Random class? (a) java.util package (b) java.lang
package (c) java.awt package (d) java.io package
xiii. What do you mean by nameless objects? (a) An object created by using the new
keyword.(b) An object of a superclass created in the subclass. (c) An object
without having any name but having a reference. (d) An object that has no
reference.
xiv. An interface with no fields or methods is known as a ______. (a) Runnable (b)
Interface (c) Marker Interface (d) Abstract Interface
xv. Which of the following modifiers can be used for a variable so that it can be
accessed by any thread or a part of a program? (a) global (b) transient (c)volatile
(d) default
xvi. If a thread goes to sleep (a) It releases all the locks it has. (b) It does not release
any locks. (c) It releases half of its locks. (d) It releases all of its lock except one.
xvii. How many threads can be executed at a time? (a) Only one thread (b) Multiple
threads (c) Only main (main() method) thread (d) Two threads