0% found this document useful (0 votes)
24 views6 pages

Arrays and OBJ

Computer science note
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
24 views6 pages

Arrays and OBJ

Computer science note
Copyright
© © All Rights Reserved
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/ 6

Arrays and Strings

Arrays: An array represents a group of elements of same data type.

Arrays are generally categorized into two types:


 Single Dimensional arrays (or 1 Dimensional arrays)
 Multi-Dimensional arrays (or 2 Dimensional arrays, 3 Dimensional arrays, …)

Single Dimensional Arrays: A one dimensional array or single dimensional array


represents a row or a column of elements.
For example, the marks obtained by a student in 5 different subjects can be
represented by a 1D array
We can declare a one dimensional array and directly store elements at the time of its
declaration, as: int marks[] = {50, 60, 55, 67, 70};

Multi-Dimensional Arrays (2D, 3D … arrays): Multi dimensional arrays represent


2D, 3D
… arrays.
 A two dimensional array is a combination of two or more (1D) one dimensional
arrays
 A three dimensional array is a combination of two or more (2D) two dimensional
arrays

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

//Displaying a 2D array as a matrix


class Matrix
{public static void main(String args[])
{//take a 2D array
int x [ ][ ] = {{1, 2, 3}, {4, 5, 6}}; // display the array elements
for (int i = 0 ; i < 2 ; i++)
{Systemoutprintln (); for (int j
= 0 ; j < 3 ; j++)
Systemoutprint (x[i][j] + “\t”);
}
}
}
Introduction to OOPs
Languages like Pascal, C, FORTRAN, and COBOL are called procedure oriented
programming languages Since in these languages, a programmer uses procedures or
functions to perform a task When the programmer wants to write a program, he will
first divide the task into separate sub tasks, each of which is expressed as functions/
procedures This approach is called procedure oriented approach.

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

Procedure Oriented Programming Object Oriented Programming

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

10 To add new data in program user 10 Message passing ensure the


should ensure that function allows it permission of
accessing member of an object from
other object

11 Top down process is followed for 11 Bottom up process is followed for


program design program
design

12 Example: Pascal, Fortran 12 Example: C++, Java

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

Object: An Object is a real time entity An object is an instance of a class.


Instance means physically happening
An object will have some properties and it can perform some actions. Object contains
variables and methods.
eg: Student s; // s is reference variable
s = new Student (); // allocate an object to reference
variable s

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

Abstraction provides security

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

Reusability of code is main advantage of inheritance In Java inheritance is achieved


by using extends keyword
The properties with access specifier private cannot be inherited

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

You might also like