Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
100%
(2)
100% found this document useful (2 votes)
403 views
99 pages
Computer Goyals
Uploaded by
Nitesh Sharma
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save COMPUTER GOYALS For Later
Share
100%
100% found this document useful, undefined
0%
, undefined
Print
Embed
Report
100%
(2)
100% found this document useful (2 votes)
403 views
99 pages
Computer Goyals
Uploaded by
Nitesh Sharma
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save COMPUTER GOYALS For Later
Share
100%
100% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save COMPUTER GOYALS For Later
You are on page 1
/ 99
Search
Fullscreen
Computer Applications Question Bank with Model Test Papers For Class 10 Semester-2 Examination 2022 vvvy CISCE’s Modified Assessment Plan for Academic Year 2021-22 Reduced and Bifurcated Syllabus for Semester-2 Examination Chapterwise Summary and Important Points Chapterwise Question Bank having all varieties of expected Questions with answer for Semester-2 Examination to be held in March-April, 2022 Specimen Question Paper (Solved) for Semester-2 Examination released by CISCE 5 Model Test Papers based on the latest Specimen Question paper issued by CISCE for Semester-2 Examination to be held in March-April, 2022 GOYAL BROTHERS PRAKASHAN NEW DELHISECTION A: QUESTION BANK UNIT 5 : LIBRARY CLASSES 4. Class Libraries 7-18 UNIT 6 : ENCAPSULATION 2. Encapsulation 19-27 UNIT 7 : ARRAYS 3. Arrays 28-43 4. Sorting and Searching 44-56 UNIT 8 : STRING HANDLING 5. Strings 57-70 71-84 6. String Manipulation SECTION B : SPECIMEN QUESTION PAPER [Released by CISCE for Semester-2 Examination to be held in March-April 2022] Specimen Question Paper (Solved) 85-92 SECTION C : MODEL TEST PAPERS [Based on the Latest Specimen Question Paper released by CISCE] © Model Test Paper 1 (Unsolved) . 93-95 © Model Test Paper 2 (Unsolved) me 96-97 © Model Test Paper 3 (Unsolved) 98-99 © Model Test Paper 4 (Unsolved) oe 100-101 © Model Test Paper 5 (Unsolved) 102-103wm UNIT 5 : LIBRARY CLASSES 1 Class Libraries IMPORTANT POINTS «A library in Java is a collection of classes — usually providing related facilities — that you can use in your programs. The Java class library provides you with a whole range of goodies, some of which are essential for your programs to work at all, and some of which make writing your Java programs easier. # Since the class library is a set of classes, it is stored in sets of files where each file contains a lass definition. The classes are grouped together into related sets that are called packages, and ach package is stored in a separate directory * A class in a package can access any of the other classes in the package. A class in another package may or may not be accessible. The package name is based on the path to the directory in which the classes belonging to the package are stored. * Classes in the package, for example java.lang are stored in the directory path java\lang, © Creating reusable, inheritable classes can save amazing amounts of time and energy, which in tur atly boosts productivity. Java itself takes code reuse to heart in its implementation of a wide Variety of standard objects that are available to Java programmers. The standard Java objects are known collectively as the Java class libraries. java lang are the classes that apply to the language itself, including the Object class, the String class, and the System class. It also contains the special classes for the primitive types (Integer, Character, Float, and so on). # java.util are utility classes, such as Date, as well as simple collection classes, such as Vector and Hashtable * java.io are input and output classes for writis and output) and for handling files. @ java.net classes are for networking support, including Socket and URL (a class to represent reference: documents on the World Wide Web). to and reading from streams (such as standard input « java.awt is the Abstract Windowing Toolkit. It contains classes to implement graphical user interface features, including classes for Window, Menu, Button, Font, CheckBox, and so on. It also includes mechanisms for mana: ‘stem events and for processing images (in the java.awt.Image package). « java.applet classes implement Java applets. « The Java language package, which is also known as java.lang, provides classes that make up the core of the Java language. The language package contains classes at the lowest level of the Java class libraries. For example, the object class, which all classes are derived from, is located in the language package # It is impossible to write a Java program without dealing with at least a few of the elements of the language package. The most important classes contained in the language package follow (ay The Object Class (b) Data Type Wrapper Classes (c) The Math Class (d) String Classes (e) System and Runtime Classes (1) Thread Classes Class Classes (hy (i) Process Classes =xeeption Handling Classes Guyals ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 7# The Object class is the superclass for all classes in Java, Because all classes are derived from Object, the methods defined in object are shared by all classes. This results in a core set of methods that all Java classes are guaranteed to support. Object includes methods for making copies of an object, testing objects for equality, and converting the value of an object to a string, © The fundamental data types (int, char, float, and so on) in Java are not implemented as classes, Many times it is usefull, however, to know more information about a fundamental type than just its value. By implementing class wrappers for the fundamental types, additional information can be maintained, as well as methods defined that act on the types. The data type wrapper classes serve as class versions of the fundamental data types, and are named similarly to the types they wrap. For example, the type wrapper for int is the Integer class, Following are the Java data type wrapper classes: (a) Boolean (b) Character (©) Double (d) Float (e) Integer (8) Long * Type wrappers are also useful because many of Java's utility classes require classes as parameters, not simple types. You can get a simple type from a wrapper through a simple method call. Using autoboxing and unboxing, now, the two types (wrapper and fundamental data types) are interchar @ The Math class serves as a grouping of mathematical functions and constants. All the variables and methods in Math class are static, and the Math class itself is final. This means you cannot derive ses from Math. Additionally, you cannot instantiate the Math class. It is best to think of as just a conglomeration of methods and constants for performing mathematical reable. new cla the Math class computations. © The Math class includes the E and PI constants, methods for determining the absolute value of a number, methods for calculating trigonometric functions, and minimum and maximum methods, among others. © For various reasons (mostly security related), Java implements text strings as classes, rather than forcing the programmer to use character arrays. The two Java classes that represent strings are String and StringBuffer. The String class is useful for working with constant strings that cannot change in value or length. The StringBuffer class is used to work with strings of varying value and length. © The System and Runtime classes provide a means for your programs to access system and runtime environment resources. Like the Math class, the System class is final and is entirely composed of static variables and methods. The System class basically provides a system-independent programming interface to system resources. Examples of system resources include the standard input and output streams, System.in and System.out, which typically model the keyboard and monitor. * The Runtime class provides direct access to the runtime environment. An example of a run-time routine is the free Memory method, which returns the amount of free system memory available. * Runtime error handling is a very important facility in any programming environment. Java provides the following classes for dealing with runtime errors: (a) Throwable (b) Exception (c) Error * The Throwable class provides low-level error handling capabilities such as an execution stack list. The Exception class is derived from Throwable and provides the base level of functionality for all the exception classes defined in the Java system, The Exception class is used for handling normal errors. The Error class is also derived from Throwable, but it is used for handling abnormal errors that are not expected to occur. Very few Java programs worry with the Error class; most use the Exception class to handle runtime errors © The Java utilities package, which is also known as java.util, provides various classes that perform different utility functions. The utilities package includes a class for working with dates, class, among others Goval’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 ExaminationThe most important classes contained in the utilities package follow: (a) The Calendar Class (b) The Date Class (c) Data Structure Class: (d) The Random Class (e) The StringTokenizer Class (0) The Properties Class (g) The Observer Interface (h) The Enumeration Interface ‘* The Date class represents a calendar date and time in a system independent fashion. The Date class provides methods for retrieving the current date and time as well as computing days of the week and month, © The Java VO package, also known as java.io, provides classes with support for reading and writing data to and from different input and output devices, including files. The I/O package includes classes for inputting streams of data, outputting streams of data, working with files, and tokenizing streams of data. The most important classes contained in the /O package follow: (a) Input Stream Classes (b) Output Stream Classes (©) File Classes (d) The StreamTokenizer Class GxQuestioneankes SECTION A 1. Tick () the correct option: (a) A package contain: (i) Tags (ii) Classes (iii) Data (iv) Arrays (b) Each primitive data type belongs to a specific: (i) Block Gi) Object (iii) Wrapper class (iv) None of these (c) Automatic conversion of primitive data into an object of wrapper class is called: (i) Autoboxing (ii) Explicit conversion (iii) Shifting (iv) None of these (d) The parselnt() function is a member of: (i) Integer wrapper class (ii) Character wrapper class (iii) Boolean wrapper class iv) None of these (e) The valueOft) function converts: (i) Primitive type to String (ii) String to primitive type (iii) Character to String iv) None of these (8) What is the significance of *** while importing @ pi (i) It lets you specify that there are 0 or 1 character only (ii) It lets you specify that the * may be replaced by any string whatsoever (ii) It is the literal ** character itself (iv) None of these (g) Differentiate between parselnt() and to§ age? ing(). (i) parselnt separates out the integer based on comma and toString joins the strin, (ii) pars (iii) parseInt retums the string converted into an integer and toString converts any object to string (iv) None of these (h) Composite data types are formed by using (i) Class variables (ii) Primitive data type tic data types (iv) Method names Int returns subparts of the integer and toString converts integers to strings Goyal’s ICSE Computer Applications Question Bank with MIP Class 10 for Semester-? Examination 910 (i) In the below code what is the value stored in res? int res = ‘A’; (i) 65 (ii) 97 (iii) 101 (iv) 110 (i) Name the package that contains wrapper classes. (i) java.util (ii) java.lang (iii) java.io (iv) java.wrap (Kk) Write the prototype of a function check which takes an integer as an argument and returns a character. (i) char check (int); (ii) Character check (integer); (iii) char check(Integer); (iv) Integer check (Character); (J) Write the purpose of Float.parseFloat(), (i) Changes the input string to float (ii) Changes the input float to string (iii) Changes the float to double (iv) None of these (m) Write the purpose of Integer.valueOf(). (i) Converts String to float (ii) Converts String to Integer wrapper object (iii) Converts int to Integer wrapper object (iv) None of these (n) Write the output of the following program snippet. char ch = ‘*/; boolean b = Character.isLetter (ch); System. out .print1n(b) ; (i) false (ii) true (iii) 1 (iv) 0 (0) Write the output of the following program snippet char c = ‘A’; int n = (int) c+ 327 System.out..printin( (char) a); @ Be Gi) (p) What is the purpose of the below function? Character. isWhitespace () Gil) ‘a’ (iv) None of these (i) Checks whether the input is a space (ii) Checks whether the input is a whitespace (iii) Checks whether the input is empty (iv) None of these (q) What is the purpose of the below function? Double. toString () (i) Converts a String type to a double (ii) Converts a String type to Double wrapper object (iii) Converts a double data to a String (iv) Converts any object to a double (6) What is the output of the following code? ch = ty’: char chr = C ‘acter. toUpperCase (ch); int p = (int) System.out.printin(chr+ “\t" + p); WY 89 (iy y 121 (iii) y 120 (iv) Y 65 (s) What is the output of the following code? char c = ‘BY; int i = em. out .print1n(cti); cy (iii) 98 (iv) 102 98, 102 (i) 70 70 Goyal’s ICSE Computer Applications Question Bank with MIP Class 10 for Semester Examinationsolution: @) Gi (b) (iii) (c) (i) @ @ (©) Gi) wo (g) (iii) (a) (ii) ow Ow &) (i) OO @ Gi @) @ (0) Gi) (p) Gi) (a) Gi) © @ ) @ SECTION B 2. Write a program in Java to input a character, Find and display the next 10" character in the ASCIL table. Solution: import java.util.Scanner; public class TenthChar { public static void main(String args(]) Scanner in = new Scanner (System.in); System.out.print ("Enter a character: "); char ch = in.next().charAt (0); char nextCh = (char) (ch + 10); System.out.println("Tenth character from + ch + " is " + nextCh); 3. Write a program in Java to input a character. Display next 5 characters. public static void main(String args{]) ( Scanner in = new Scanner (System.in); harac! Bia ystem.out.print ("Enter a snext () charat (0); xt 5 cha’ rs from " are:"); for (int i = 1; i <= 5; i++) System.out.print1n(++ch); tin ("N 4. Write a program in Java to generate all the altemate letters in the range of letters from A to Z. class AlternateLetters public static void main(String args{]) ( for (char ch = 'A'; ch <= 'Z'; ch = (char)(ch + 2)) { System. out.printin (ch); Goyal’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination u5. Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display the number of vowels and number of consonants present in the set of given letters. Solution: import java.util.Scanner; public class Letterset20 ( public static void main(String args{]) { Scanner in = new Scanner (System.in); System.out.printin("Enter any 20 letters"); int ve = 0, cc = 0; for (int i = 0; i < 20; i++) { char ch = in.next().charAt (0); ch = Character. toUpperCase (ch) if (ch AT Lt ch ‘eT ch oe tl ch ovoid ch ‘uy vett; else if (ch >= && ch <= 'z") cott: ) System.out.println("Number of Vowels = " + ve); System.out.print1n("Number of Consonants = " + cc); ) 6. Write a program in Java to accept an integer number N such that 0
0 G6 mw < 27) { har ch = (char)(n + 64); System. out.print1n ("Corresponding letter = “ + ch); ) else ( ystem.out.println ("Please enter a number in 1 to 26 range"); } “SE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 27. Write a program to input two characters from the keyboard. Find the difference (4) between their ASCII codes. Display the following messages: If d=0 : both the characters are same. If d<0 : first character is smaller. If d>0 : second character is smaller. Sample Input : D P Sample Output : (68-80) = -12 First character is smaller Solution: import java.util.Scanner; public class ASCIIDif£ { public static void main(String args[]) { Scanner in = new Scanner (System.in); System.out.print ("Enter first character: ")i char ch! in.next ().charAt (0); System.out.print ("Enter second character: ") char ch2 = in.next().charAt (0); int d = (int)chl - (int)ch2; if (a > 0) System.out.println ("Second character is smaller"); élse if (a < 0) System.out.println("First character is smaller"); else System.out.println("Both the characters are same"); 8 Write a program to input a set of any 10 integer numbers. Find the sum and product of the numbers, Join the sum and product to form a single number. Display the concatenated number. [Hint: let sum=245 and product = 1346 then the number after joining sum and product will be 2451346] Solution: import java.util.Scanner; public class SumProdConcat public static void main(String args{]) {( Scanner in = new Scanner (System. in) system.out.printIn("Enter 10 integers"); long sum = 0, prod = 1; for (int i = 0; i < 10; itt) { int n = in.nextInt ()7 sum 4 prod ny ) i String s = Long.toString(sum) + Long.toString (prod); long r = Long.parseLong(s); System.out.println("Concatenated Number = " + r); Goyal’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 139. Write a menu driven program to generate the upper case letters from Z to A and lower case letters from ‘a! to 'z' as per the user's choice, Enter 'I' to display upper case letters from Z to A and Enter '2' to display lower case letters from atoz. Solution: import java.util.Scanner; public class Letters d public static void main(String args{]) { Scanner in = new Scanner (system. in); i: System.out.printin ("Enter '1' to display upper case letters from 2 to A"); System. out .printin("Enter '2' to display lower case letters froma to 2"); System. out.print ("Enter your choice: int ch = in.nextInt(); int count = 0; Ve switch (ch) { case 1: for (int i = 907 i > 64; char ¢ = (char) i; System. out .print (c) ; System.out.print (" count++; //Print 10 characters per line if (count == 10) { System. out.printin(); count } break; case for (int i = 97; i < 123; itt) { char ¢ = (char)i; System. out.print (c); System.out.print(" "); count++; //Print 10 characters per line if (count == 10) { System.out.printIn(); count = 0; } } break; default: System.out.print1n("Incorrect Choice"); 4 Goyal’ ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination10, Write a program to input a letter. Find its ASCII code, Reverse the ASCII code and display the equivalent character, Sample Input: Y Sample Output: ASCII Code Reverse the code = 98 Equivalent character: b Solution: import java.util.Scanner; public class ASCIIReverse ( public static void main(String args(]) { Scanner in = new Scanner (System.in); System.out.print ("Enter a letter: ")s char 1 = in.next().charat (0); int a = (int)l; System.out.printin ("ASCII Code = " + ali int r= 0; while (a > 0) { int digit = a & 10; =r * 10 + digits a /= 10; ) System.out.printin ("Reversed Code = " + r)i System.out.println("Equivalent character = " + (char)z) ) 11. Write a menu driven program to display (i) first five upper case letters (ii) last five lower case letters as per the user's choice, Enter 'I' to display upper case letters and enter '2' to display lower case letters. Solution: import java.util.Scanner; public class MenuUpLowCase ( public static void main(String args[]) ( Scanner in = new Scanner (System.in); System.out.printin("Enter '1' to display upper case letters"); system.out.printin("Enter '2' to display lower case letters"); Goyal ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 1sSystem.out.print ("Enter your choice: "); int ch = in,nextInt ()7 switch (ch) [ case 1: for (int i = 65; i <= 69; itt) System.out.printin((char)i)s break; case 2: for (int i = 118; i System. out .printin((char) i)? <= 1227 i++) break; default: break; t 12. Write a program to perform some mathematical computations and display the results. Then, report the number of seconds that the computer spent on this task. Solution: import java.math.*; class TimedComputation { public static void main(String!) args) public long startTime; // Starting time of program, long endTime; // Time when computations are done, in milliseconds. // Time difference, in seconds. System.currentTimeMillis(); height, hypotenuse; { in milliseconds. double time; startTime = double width, // sides of a triangle width = 42.0; height = 17. hypotenuse = Math.sqrt(width*width + height*height); sides 42 and 17 has ‘A triangle with System. out.print hypotenuse"); System. out. print 1n (hypotenuse) ; System.out.println("\nMathematically, sin(x)*sin(x) + check this for x = + cos(1)*cos(1) - 1 is "); c0s(x)*cos(x) should be 1 System.out.printin ("Let's System.out.print ("sin(1)*sin(1) th MTP Class 10 for Semester-2 Examination — Govats ICSE Computer Applications Question Bank wsystem.out-printin(Math.sin(1)*Math.sin(1) + Math.cos(1)*Math.cos(1) - 1); System.out.printin("(There can be round-off errors when computing with real numbers!)"); System.out.print("\nHere is a random number system. out.printin (Math. random()) + endTime = System.currentTimeMillis (); time = (endTime - startTime) / 1000.0; System.out.print("\nRun time in seconds was system. out .print1n (time); ) // end main() // end class TimedComputation 13. Write a program that outputs the results of the following evaluations based on the number entered by the user. (a) Natural logarithm of the number. (b) Absolute value of the number. (c) Square root of the number. (d) Random number between 0 and 1. Solution: import java.io. BufferedReader; import java.io, IOException; import java.io. InputStreamReader; import java.io.*7 import java.lang.*; import java.math.*; public class q_06_7 static double logarithm, absolute, public static void main(String q { double n; BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); System.out.printin ("Enter the numbe n = new Double (br. readLine())7 out.printIn ("The Natural logarithm of number is" + Math.log(n)); System System.out.println("The Absolute value of number is" + Math.abs(n)); + Math. sart (n)); System.out.print1n("The Square root of number i System.out.print1n ("Random number between 0 and 1" + Math.random()); Goyal’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 714. Make a program to generate a plot of the function: y =e-0.1t sin (0.5t) on the printer, using an asterisk (*) for each of the points that makes up a plot. Have the plot run vertically down the page, with one point *(one asterisk) per line. Note: Each printed line should consist of one asterisk by rounding the value of y to the nearest integer, scaled to the maximum number of characters per line. Solution: import java.math.*; public class plot { public static void main(String s{]) ( double y; int t; for (t=1;t<10;t++) ( System. out.print ("\n") y = Math.exp( (double) -0.1*t)*Math.sin( (double) 0.5*t for (double k=-8;k
Examinationpublic static void main(String{] args) throws 10Exception, NumberFormatException f SumOfSquares sos = new SumOfSquares(); sos. takeInputs (); s0s.assess(); ) 4, Enter five integers from the user. You have to evaluate the sum of squares of these numbers and sum of cubes of these numbers. You must print “Squares is higher” if the sum of squares is higher and “Cubes is higher” if the sum of cubes is higher. Solution import java yee * write a description of class SquaresOrCubes here. * @author (your name) * @version (a version number or a date) pub: { class SquaresOrCubes private int squares; private int cubes; ] nums; private pe * Constructor for objects of class SquaresOrCubes public SquaresOrCubes () // initialise instance variables squares = 0; new int(5)s public void takeInput() throws IOException, NumberFormatException BufferedReader bf new BufferedReader (new InputStreamReader (System. ind); for (int nums Integer.parseInt (bf. readLine()); public void assess() { Goyal’ 1CSE Computer Applications Question Bank with Mt Class 10 for Semester-2 Examination 25for(int i = 0; i < 5; i++) { squares += (nums[i]*nums[i]); } for(int i = { i <5; itt) cubes += (nums{i]*nums[i]*nums[i]); , if (squares > cubes) ( System.out .print1n ("Squares is higher"); ) else { if (cubes > squares) { System.out.printIn(*Cubes is higher”); } else System. out.println (*Equal”) ; public static void main(String{] args) throws IOException, NumberFormatException { SquaresOrCubes soc = hew SquaresOrCubes(); soc.takeInput (); soc.assess(); } 5. Take two positive integers as input from the user. Use the Euclid’s algorithm to find the HCF of these two numbers. Euclid’s algorithm works like this. Let the numbers be 12 and 20. So, a= 12, b= 20. Dividing b by a, gives, 8 as remainder. So, now a= 8, b= 12 Dividing b by a, gives, 4 as remainder. So, now a=4, b= 8 As a completely divides b, a is the HCF of the numbers. Solution import java.io.*7 ye * write a description of class FindHCF here. * @author (your name) * @version (a version number or a date) ” public class FindHCF { // instance variables - replace the example below with your own private int a; 26 Goyal’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examinationprivate int b; yo * Constructor for objects of class FindHCF ” public FindHCF() { // initialise instance variables or o7 public void takeInput() throws IOException, NumberFormatException { BufferedReader bf = new BufferedReader (new InputStreamReader (System. in)): a = Integer.parseInt (bf.readLine()); b = Integer.parseint (bf.readLine()); public int hef() { int he = b8a; while (he != 0) { bea; a= he; he = b’az } return a; } public static void main(String] args) throws IOException, NumberFormatException { FindHCF fh = new FindHCF(); fh.takeInput (); System.out.print1n("HCF of the two entered numbers is “+fh.hef()); (aa con coat neces Oana vee an wae an ee ene ”FF ree UNIT 7: ARRAYS) 3 Arrays IMPORTANT POINTS © The Java class libraries are implemented as packages, which contain groups of related classes. * Standard Java packages also include interfaces, exception definitions. * Java implements text strings as classes. © The two Java classes that represent strings are String and StringBuffer. ¢ Java provides the following classes for dealing with runtime errors: 1. Throwable 2. Exception 3. Error * Array is a linear data structure that stores elements at contiguous memory locations that is array elements are located one after the other. The array stores a fixed size sequential collection of elements of the same type. * Arrays can contain any type of element value (primitive types or objects), but you cannot store different types in a single array. You can have an array of integers or an array of strings or an array of arrays, but you cannot have an array that contains, for example, both strings and integers. * The cells are numbered sequentially starting at zero. If there are N cells in an array, the indices will be 0 through N-1. Sometimes the index is called a subseript Subsripp—> 9 pd Elements —> Stored ina cell | 1 | 20 | 30 | | * The value stored in a cell of an array is sometimes called an element of the array. An array has a fixed number of cells, but the values placed in those cells (the elements) may change over time. © An array is basically of following two types: 1, Single Dimensional Arrays 2. Two-Dimensional Arrays * When the elements are specified by a single index, it is called single dimensional array. Index ——> o 1 2 3 4 5 6 7 8 9 Elements —> | 23 | 38 | 14 | -3 Hees eo, E1030 1 | 26 Lt a © The first step in creating an array is creating a variable that will hold the array, just as you would any other variable. Array variables indicate the type of object the array will hold Gust as they do for any variable) and the name of the array, followed by empty brackets ({]). The following are all typical array variable declarations: Syntax:
(J; or
[]
; == The second step is to create an array object and assign it to that variable. There are two ways to do this; (a) Using new operator. (b) Directly initializing the contents of that array. Goyalis ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2+ A two-dimensional array is represented as a table of elements, with rows and columns that is in matrix form. The number of elements in two-dimensional array can be determined by multiplying number of rows with number of columns. Two-dimessional array is a simple form of multidimensional array, ‘The syntax for declaring and creating a two-dimensional array is: datatype [][] array name = new type [row_length] [column_length]; # Once you have an array with initial values, you can test and change the values in each slot of that array. To get at a value stored within an array, use the array subscript expression ({ }): myArray[subscript]; ‘The myArray part of this expression is a variable holding an array object, although it can also be an expression that results in an array. The subscript part of the expression, inside the brackets, specifies the number of the slot within the array to access, Array subscripts start with 0. So, an array with 10 elements has 10 array slots accessed using subscript 0 to 9. + Java SE 5.0 introduced a powerful looping construct that allows you to loop through each element in an array as well as other collections of elements, The enhanced for loop: for (variable : collection) statement sets the given variable to each element of the collection and then executes the statement (which, of course, may be a block). The collection expression must be an array or an object of a class that implements the Iterable interface, such as ArrayList. © To assign an element value to a particular array slot, merely put an assignment statement after the array access expression: myarray(1) = «# Arrays of references to objects, as opposed to the objects themselves, are particularly useful because you can have multiple references to the same objects both inside and outside arrays. For example, you can assign an object contained in an array to a variable and refer to that same object by using either the variable or the array position. # An expression such as data[3] is called a subscripted variable. A subscripted variable can be used anywhere as an ordinary variable of the same type can be used, * An arithmetic expression can contain a mix of literals, variables, and subscripted variables. For example, if x contains 10, then (x + data[2]) / 4 evaluates to (10+14) / 4 = 6. Here are some other legal statements: data{0] = (x + datal2]) / 47 data[2] = data[2] + 17 x = data[3]++ ; // the data in cell 3 is incremented datal4 data[1] / data(6]; ‘* The elements of the array were processed one after the other in the sequence in which they occur in the array. But one of the big advantages of arrays is that they allow random access. That is every element of the array is equally accessible at any given time. In java.util, you will find the Arrays class, which holds a set of static_methods that perform utility functions for arrays. There are four basic functions: equals (), to compare two arrays for equality; £111 (), to fill an array with a value; sort (), to sort the array; and binarySearch(), Goyal’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 29to find an element in a sorted array. All of these methods are overloaded for all the primitive types | and Objects. In addition, there is a single asList () method that takes any array and tums jt into a List container, © The Java standard library provides a static method, system.arraycopy (), which can make | much faster copies of an array than if you use a Eor loop to perform the copy by hand. System, arraycopy () is overloaded to handle all types, Here is an example that manipulates arrays of int k. length) ; System.arraycopy(i, 0, k, SECTION A 1, Tick (¥) the corteet option: (a) Write a valid statement to declare a single dimensional array of 5 integers. (i) int ar[S] = new int(5]s (ii) int ar(6] = new int{6); (iii) int ar[4] = new int{4]; (iv) None of these (6) What do you mean by a sub: (i) A positive integer enclos (ii) It is used to access elements (iii) Both (i) and (i). (iv) Used to declare class variables (c) What is the starting row and column indices of the array m[3][4]? (i) 0 and 0 respectively (ii) Land 1 2 and 3 (iv) 3 and 4 (4) What is printed by the below statements? int Num(] = {3, 5, 10, 12, 8}; T#0\e" + Num[1)+"\e"+Num[3]+"\t" + Num[4-2]); cript? d by square brackets is known a of arrays: subscript System. out.printin (Nu @3 5 10 G10 5s 22 jis 10 122 8 (iv) None of these () What do you mean by subscripted variable? (i) The variable after initialization using constructor (ii) The variable represented by the subscript of an array (iii) The last element of an array (iv) None of these () From the given array Y [If] = (13, 4, 5}, The element at Y[2][2] is: @ (ii) 2 Gi) 3 (iv) 4 (g) Write a set of statements that would find and print the size of array ar{] = (2, 5, 6, 7, 8 9, 10}; (i) int len = arlength; System. out println(“The size of arra (ii) int len = arlength(; ‘+len); Goval’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 ExaminationSystem.out printin(““The size of array="+len); (iii) int len = arden; System,out.printIn(“The size of array="+len); (iv) None of these (h) The first value in the ini izer list of an array corresponds to index: @0 (i) 1 (iii) length -1 (iv) length (i) Which of the following cannot have the array type? (i) Integers (ii) Floats (iii) Strings (iv) None of these (j) Suppose there is an array as a part of class definition. If you now initialize the first element of the array, it will give error. Why? (i) As array elements cannot be initialized. They take default value. (ii) As array elements only take Boolean values. You did not use Boolean. (iii) As array has to be initialized using new, before initializing subscripted variable (iv) None of these (k) If you have a 2 dimensional array, h, and initialize it as h = new int[6]; It gives an error, Why? (i) It is 2 dimensional . So must be initialized with , h = new int{][], with values within square brackets. Not as new int(6]. (ii) It does not know what values to fill in the elements (iii) The subscripted variable does not need initialization. You can directly use. (iv) None of these (1) Does the constructor automatically initialize an array? (i) Yes (ii) No (iii) Sometimes (iv) It depends (m) In the below two lines of Java, what is the length of the array lastNames? String[] firstNames = (“Raman”, “Amir”, “Kapil”, “Amit” String[] lastNames = new String[firstNames.length]; (i) 3 (ii) 4 (iii) 5 (iv) None of these Solution: @ @ ogi) OF) OM Of OF) We (hy (i) am@ OG) WO Gi) (mM Gd SECTION B 2. Write a program that store numbers of a 3 by 4 matrix in two-dimensional array and display sum of all the numbers of the matrix. class sumMatrix public static void main (String args{]) i, 4, s-Oe ALLL] = {13,4,6,7}, (863-2,9}, (6, 7,5,2}}7 for (i=0; i<3; itt) for (j=0; 4<47 j++) Goyal’s ICSE Compuer Applications Question Bank with MTP Class 10 for Semester-2 Examination uu) } System.out.printin(" The sum of elements of the two-dimensional array iss" + 8 7 } b 3. Write a program that store elements in two matrices in two-dimensional array and display the addition of two matrices. Solution: port java.lang.*; import java.io. public class addmatrix ( static void add() { int a(](/bOU-cO Us a = new int [2] (217 b = new int(2](2}7 © = new int(2](2]7 DataInputStream dts = new DataInputStream(System.in); try { System.out.println("enter first matrix 2*2 order:"); for(int i=0;i<2;i++) { for(int j= 22 j++) { ali] (jl Integer.parseInt (dts.readLine()); 1 Systen.out.printin("enter second matrix 22 : for(int. ind; $2) $44) { for(int 5=0;j<2;j++) DILIJ] = Integer.parsernt (ats. readhine ()); I for (int te for int a { efi ti] + blillile } } for int iy { for(int j++) 2 Gals 136 Computer Aplications Quston ark with MTP Clas 10 or Ser 2 Banosystem.out.print (cil (j]#" "); } system.out.printin("\t"); ) ) catch (Exception x) ‘ system.out.printin ("error"); t } public static void main(String arg(}) ( add (0+ ) ) Now we will see a program that takes as input integers into a two-dimensional array and provide as output the sum of elements of a particular row and column. Write a program to enter data into a 5 x 5 two-dimensional array. Then, print out the sum of elements of the 4" row and the 3“ column. Solution: import java.io. *; public class MatrixSum static void main(String args{]) throws IOException int num{][] = new int 5] [5]7 BufferedReader br = new BufferedReader (new InputStreamReader (System. in)) 7 for (int i = O;i<5;i++) { for (int j = 074 < { num[i]{j] = Integer.parseInt (br.readLine()}; } } // The task now find out the sum of elements of row r = 3 and later // elements of 2 int x = 3, int sum_row for (int i sum_ow += num{r] (il; ) System.out.println("Sum of elements of row "+r +” is "+sum_row)i for(int i = O;i< 5;it+) ( sum_col += num{i] {co}; ) System.out.print1n("Sum of elements of column "tc + “is” +sum_col); : s joval’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-? Examination 335. Write a program to take as input the elements of a 5 x 5 two-dimensional array. Calculate sum of the right diagonal and left diagonal elements of the array and display the sums, Solution: import java.io.*; ’ public class Diagonal_sum { public static void main(String args{]) { int num{}{] = new int (5) (5); BufferedReader br = new BufferedReader (new InputStreamReader (System. in)); for (int i = 0;i<5;i++) { for (int j { num[i][3] = Integer.parseInt (br. readbine()); } } // To compute the sum of left diagonal elements int sum_left_d = 0,sum_right _d = 0; for (int i= O;i < 5;it+) { sum_left_d += num[iJ [i]; _ OF} < Srj++) for(int I = 0;i<5;i++) { sum_right_d += num[iJ(5 - i - 1]; } System.out.printin ("Sum of left diagonal elements = “+sum_left_d); System.out.print1n("Sum of right diagonal elements = "+sum_right_d); } 6. Write a program to take input from the user, by using a single subscripted variable, the number of, students and marks in Computer Applications of those students. Print those marks in descending order in a column. Solution: import java.io.*; public class desc { Public static void main(String q[])throws IOException { BufferedReader br = new BufferedReader (new InputStreamReader (System. in)); int marks[] = new int[40]; System.out.printin("Enter no. of students"); int nj n = Integer.parseint (br. readLine()); System.out.println ("Enter marks of "+ n + "™ students"); for(int i=0; isn; i++) marks[i] = Integer.parseInt (br.readLine()); for(int i=0; icn; i+t) { for(int 3 jsn-iz j++) Bs ea il E a 34 Goyal’s ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination{ if(marks(j] < marks(3+11) { int temp = marks(]; marks(j] = marks(+1]; marks (j+1] = temp; ) ) System.out.print1n("Descending order is"); for(int is0;icnsi++) ( system. out.print1n (marks [i]) + ) ) ) 7. Write a generalised program to take a proverbial statement (like "Failures are the pillars of success” as an input from the user and then output the frequency of the vowels occurring in the statement. Solution: import java.io.*; public class FreqVowel public static void main(String a{])throws IOException { BufferedReader br = new BufferedReader (new InputStreamReader (System. in)); String str=new String()i System.out printIn("Enter new String"); int n{] = new int(5]; for(int i=0; i<5; it+) alil = 07 int number = 0; str x. readLine(); int s = str.length(; system. out .println(s) + for (int ics; itt) { if(str.charAt (i) tat) lO] +47 else if(str.charAt (i) te’) n(.]++s else if(str.charAt (i) n[2]++7 else if(str.charAt (i) a3] +47 else if(str.charAt (i) tury n(4]++; Gopal’ ICSE Computer Applications Question Bank with MTP Class 10 for Semester-2 Examination 38
You might also like
0llcomputer Applications ICSE 10th Answer PDF
PDF
100% (1)
0llcomputer Applications ICSE 10th Answer PDF
372 pages
Computer Project Class 10 Icse Programming Divine Mercy
PDF
100% (1)
Computer Project Class 10 Icse Programming Divine Mercy
49 pages
Computer Application Sic Se
PDF
100% (1)
Computer Application Sic Se
112 pages
642oswaal ICSE Mind Maps On Tips Notes Class 10 Computer Applications
PDF
No ratings yet
642oswaal ICSE Mind Maps On Tips Notes Class 10 Computer Applications
9 pages
Computer Applications Icse10th Ans M
PDF
No ratings yet
Computer Applications Icse10th Ans M
371 pages
ICSE BlueJ Theory
PDF
80% (41)
ICSE BlueJ Theory
24 pages
5 6212845557178697234
PDF
No ratings yet
5 6212845557178697234
109 pages
Exam18 ICSE Sample Paper English Language 1
PDF
100% (1)
Exam18 ICSE Sample Paper English Language 1
9 pages
Arrays: A. Answer The Following Questions: Ans
PDF
No ratings yet
Arrays: A. Answer The Following Questions: Ans
56 pages
Exam18 Computer Applications Sample Prelim Paper PDF
PDF
No ratings yet
Exam18 Computer Applications Sample Prelim Paper PDF
5 pages
Icse Physics Mcqs 2025
PDF
No ratings yet
Icse Physics Mcqs 2025
3 pages
ICSE Class 10 Computer Applications (Java) 2011 Solved Question Paper - ICSE J
PDF
0% (1)
ICSE Class 10 Computer Applications (Java) 2011 Solved Question Paper - ICSE J
10 pages
Exam18 ICSE Sample Paper Computer Applications PDF
PDF
No ratings yet
Exam18 ICSE Sample Paper Computer Applications PDF
7 pages
ICSE Class 10 Computer Applications 2012
PDF
No ratings yet
ICSE Class 10 Computer Applications 2012
4 pages
Exam18 ICSE Sample Paper Biology PDF
PDF
No ratings yet
Exam18 ICSE Sample Paper Biology PDF
10 pages
Computer Applications Icse
PDF
100% (1)
Computer Applications Icse
134 pages
Sample Papers: Icse Class 10
PDF
No ratings yet
Sample Papers: Icse Class 10
8 pages
Oswal Gurukul Most Likely Chemistry Class 10 Icse-Compressed
PDF
100% (2)
Oswal Gurukul Most Likely Chemistry Class 10 Icse-Compressed
327 pages
Comp Smart Notes
PDF
No ratings yet
Comp Smart Notes
94 pages
ICSE Class 10 Computer Applications Reduced Syllabus 2020-21
PDF
No ratings yet
ICSE Class 10 Computer Applications Reduced Syllabus 2020-21
4 pages
Question Bank Solutions For ICSE Class 10 - CISCE
PDF
No ratings yet
Question Bank Solutions For ICSE Class 10 - CISCE
5 pages
Exam18 ICSE Sample Paper Maths
PDF
0% (1)
Exam18 ICSE Sample Paper Maths
9 pages
ResPaper ICSE Class X Prelims 2024 Physics
PDF
No ratings yet
ResPaper ICSE Class X Prelims 2024 Physics
7 pages
10 ICSE Chemistry Give Reason
PDF
100% (1)
10 ICSE Chemistry Give Reason
29 pages
Icse Class-Xth-Physics Model Paper
PDF
100% (1)
Icse Class-Xth-Physics Model Paper
4 pages
Arihant - Computer Sample Paper 13 ICSE 2025
PDF
No ratings yet
Arihant - Computer Sample Paper 13 ICSE 2025
5 pages
1537177499-0llcomputer Applications ICSE 9th Answer PDF
PDF
100% (2)
1537177499-0llcomputer Applications ICSE 9th Answer PDF
207 pages
Frank Computer Book ICSE - 621f7d0b C42a 4325 Ade8 f2cf517c7cdd
PDF
100% (1)
Frank Computer Book ICSE - 621f7d0b C42a 4325 Ade8 f2cf517c7cdd
440 pages
Icse Tips
PDF
No ratings yet
Icse Tips
6 pages
Computer ISCE Sample Paper
PDF
100% (1)
Computer ISCE Sample Paper
5 pages
Computer NOTES ICSE 10 2024
PDF
No ratings yet
Computer NOTES ICSE 10 2024
141 pages
Class X (61 Sample Papers)
PDF
0% (2)
Class X (61 Sample Papers)
38 pages
Don Bosco Park Circus Class 10 Physics Preboards Paper Icse 2024
PDF
No ratings yet
Don Bosco Park Circus Class 10 Physics Preboards Paper Icse 2024
7 pages
Computer Applications Sample Paper ICSE 2020 PDF
PDF
No ratings yet
Computer Applications Sample Paper ICSE 2020 PDF
11 pages
Biology IMP Notes PDF
PDF
100% (4)
Biology IMP Notes PDF
159 pages
Bio Q Bank Icse Class 10
PDF
100% (1)
Bio Q Bank Icse Class 10
34 pages
ResPaper ICSE Class X Prelims 2024 Computer Applications (Loyola School Jamshedpur)
PDF
No ratings yet
ResPaper ICSE Class X Prelims 2024 Computer Applications (Loyola School Jamshedpur)
8 pages
Complete Hios Pyq
PDF
No ratings yet
Complete Hios Pyq
107 pages
Chapter 7-Conditional Statement
PDF
No ratings yet
Chapter 7-Conditional Statement
38 pages
Exam18 ICSE Mathematics Sample Paper
PDF
No ratings yet
Exam18 ICSE Mathematics Sample Paper
9 pages
50 Most Important Programs For Computer Class 10 Icse
PDF
No ratings yet
50 Most Important Programs For Computer Class 10 Icse
54 pages
Exam18 ICES Class 10 Prelim Papers Package 2020 Set 1 Digital Download - 5da6de8359729 - e PDF
PDF
50% (2)
Exam18 ICES Class 10 Prelim Papers Package 2020 Set 1 Digital Download - 5da6de8359729 - e PDF
276 pages
ICSE Class 10 Maths Probability
PDF
No ratings yet
ICSE Class 10 Maths Probability
5 pages
Computer App With BlueJ-9 - TM - 2023
PDF
100% (2)
Computer App With BlueJ-9 - TM - 2023
128 pages
Biology 7 Class 9
PDF
100% (5)
Biology 7 Class 9
203 pages
Chemical Bonding ICSE Class-10 Concise Chemistry Selina Solutions - Page 2 of 5 - ICSEHELP
PDF
No ratings yet
Chemical Bonding ICSE Class-10 Concise Chemistry Selina Solutions - Page 2 of 5 - ICSEHELP
9 pages
Goyal's ICSE 10th Biology Model Papers
PDF
No ratings yet
Goyal's ICSE 10th Biology Model Papers
39 pages
Icse Maths Goyel Sem 2 by Swapan
PDF
100% (2)
Icse Maths Goyel Sem 2 by Swapan
153 pages
Computer Application - Rehearsal Examination - 2023-24
PDF
100% (2)
Computer Application - Rehearsal Examination - 2023-24
11 pages
Notes of Bluej For STD 10
PDF
100% (2)
Notes of Bluej For STD 10
17 pages
10th Picture Questions Biology 10th ICSE
PDF
No ratings yet
10th Picture Questions Biology 10th ICSE
43 pages
ICSE Class 10 Chemistry Question Paper 2023
PDF
No ratings yet
ICSE Class 10 Chemistry Question Paper 2023
11 pages
X Chem Master Key Differences 23 - 24
PDF
No ratings yet
X Chem Master Key Differences 23 - 24
35 pages
Xavier School, Gamharia 2nd TERM EXAM2020: Class - X F.M-80 Sub - Physics Time: 2 Hrs
PDF
100% (2)
Xavier School, Gamharia 2nd TERM EXAM2020: Class - X F.M-80 Sub - Physics Time: 2 Hrs
6 pages
Packages
PDF
No ratings yet
Packages
38 pages
Java Basics
PDF
No ratings yet
Java Basics
23 pages
coreAPI 2x2
PDF
No ratings yet
coreAPI 2x2
28 pages
Java Intro
PDF
No ratings yet
Java Intro
25 pages
Unit 4
PDF
No ratings yet
Unit 4
57 pages
3 - JAVA Characteristics
PDF
No ratings yet
3 - JAVA Characteristics
55 pages