0% found this document useful (0 votes)
18 views

Computer revision 1

The document provides a comprehensive overview of Java programming concepts, including object-oriented principles such as encapsulation, inheritance, and polymorphism. It covers fundamental Java syntax, data types, operators, control structures, and the Java Virtual Machine's role in executing Java programs. Additionally, it includes multiple-choice questions and programming exercises to reinforce understanding of Java concepts.

Uploaded by

akarshhagarwal
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
0% found this document useful (0 votes)
18 views

Computer revision 1

The document provides a comprehensive overview of Java programming concepts, including object-oriented principles such as encapsulation, inheritance, and polymorphism. It covers fundamental Java syntax, data types, operators, control structures, and the Java Virtual Machine's role in executing Java programs. Additionally, it includes multiple-choice questions and programming exercises to reinforce understanding of Java concepts.

Uploaded by

akarshhagarwal
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
You are on page 1/ 22
t REVISION Java Concepts 1. An object is an identifiable entity that ha: similar characteristics and behavior. Itis a blueprint/proto .s some characteristics and behavior. type of objects -kground details. 2, Aclass isa group of objects havin; 3, Data abstraction is the act of representing essential features and ignoring the bac 4, Encapsulation is the wrapping up of data and functions operating on the data into a single unit, called a class. itis a way of implementing abstraction. 5, Inheritance is the ability of one class to inherit inherited is called the super/base class and the class that is inheriting is cal 6. Polymorphism is the ability of an object to take many forms, It allows a single task it the properties of another class. The class that is being led the sub/derived class. to be performed in different ways. 7. Modularity is the property of breaking up a program into smaller components. 8, When a Java program is executed, the compiler compiles that piece of code (source code) and a Bytecode is generated. It is platform-independent. 8, The Java Virtual Machine (JVM) reads the bytecode and interprets it into machine code depending upon the operating system and hardware available. Java Fundamentals 4. Java uses the UNICODE character set which is a 2-byte character code set that can represent all characters in almost all human language alphabets. 2, Tokens are the smallest individual units in a program. Java has 5 types of tokens: keywords, identifiers, Jiterals, operators and punctuators. 3, Keywords are reserved words that convey a special meaning to the compiler. They cannot be used as identifier names. 4, Identifiers are names given to th ‘They must not be a keyword, boolean literal or null. 5, Literals are fixed data values. They can be of 6 types: Integer, Floating-point, Character, String, Boolean and Null. 6, Operators are special symbols that cause an action to take place. There are 37 of them in Java. different building blocks of a program, eg, class, method, variables, etc. 7, Punctuators are used to group and separate the data values. Touchpad Question Bank (CA 86)-X 8, Data types : They are means to identify the type of data and the associated operations of handling it. Java has 2 categories of data types: © Primitive datatypes: They are in-built. The 8 types are byte, short, int, long, float, double, char and boolean. ‘Reference datatypes: They are constructed of primitive datatypes, e.g., classes, arrays. 9. A variable is a named memory location that holds a value of a particular data type. It has to be declared before being used. Eg: int num; Itcan be initialised at the time of declaration. Eg: intnum=12; Sometimes in a program you may want to give a name to a constant value, e.g, maxmarks=100. The value of these constants must not change in the program. ‘The keyword final is used to declare a constant. Eg: final int maxmarks=100; 10. Comments are statements that are not executed by the compiler. They are used to provide information or explanation to the programmer. Single-line commen eT Multi-line comments: /*...*/ 11, Operators Unary Operators: They act on a single operand. Eg, +5, -8 Binary Operators: They act on 2 operands. E.g, 5+7, 6*3 Arithmetic Operators: ae % Relational Operators: < <= > > Logical Operators: && || ! (unary) Assignment Operator: =eg,a=b Shorthand assignment operators: * += eg,a+=3 meansa=at3 Increment Operator Eg, a+ OR ++a both mean a-at1 + Prefix increment is ++a where the value of ais first changed and then used in the expression. * Postfix increment is a++ where the value of a is first used in the expression and then changed. Revision @ © Decrement Operator: Eg, a-OR-aboth mean a=a-1 © Conditional Operator is ?: and is also called ter Syntax: expression! ? expression2 : expression3; Eg, c=a>b?a:b; System.out printin(“Larger number" +a>b?: 12. Operator precedence determines the order in which expressions nary operator as it acts on 3 operands. are evaluated. jons are a valid combination of operators, constants and variables. ons: all operands are of the same data type. ed expressions: operands are of different data types. the resultis either true or false. converting one predefined data type into another. ++ 7eie1990 3 tere cusin Hadie+ AO +~s int a=in.nextInt (); System.out.print1n("Enter a double number "); double d=in.nextDouble (); System.out.print1n("Enter a word "); String s-in.nextLine(); System.out.println("Enter a character char c=in.next ().charAt (0); ) Math Class Java has in-built mathematical functions that are found in the Java standard library. They are available through the Math class defined in the java.lang package. Basic Math methods Math.abs() | Itwill return the absolute value of the given value. Itreturns the largest of two values. ment. It is used to find the largest integer value which is less than or equal to the It returns a double value with a positive sign, greater than or equal to 0.0 and less than 1. Itreturns the double value that is closest to the given argument and equal to mathematical integer. of conditional constructs for programming such as the if statement, if-else statements and \boden mqoataaiionsen|iicou-qool ¢ rsciw zi qool b: a ° statement 1; // executed if the expression is true else statement2; // executed if the expression is false Switch statement Syntax: switch (expression) { case constant1 statement/s; break; V1 the case corresponding to the matching value of the expressic, case constant2 : i statement/s; | break; // is executed default Statement/s; // an optional statement that is executed when no match is found Iterative Constructs in Java To repeat a set of statements, Java provides a variety of iterative constructs such as for, while and do-while loop. for loop Synta: for( initialization expression; { : statements; // body of the loop test expression; increment/decrement expression) 9.0.09 Saupe 40 neds "statements; // body of the loop TS fecaps thas sromugte o-.' do..while loop Syntax: do{ statements; | // body of ‘the loop } while(test expression); Anested loop is when a loop contains another loop in its body. 018) Touchpad Question Bank (CA 86)-X Jump statements break is used to terminate a loop. It can also be used in a switch construct to terminate a case. continue is used to abandon the current iteration of a loop and proceeds to the next iteration. Objective Questions ————————_ [HE] Multiple Choice Questions (MCQs) f// 1. Which of the following is not a valid literal? a5 ca 2. Which of the following is not a keyword? a. void b. for c. class d. object 3, What is the code generated when a Java program is compiled? a. machine code b. source code c. object code d. bytecode 4. What is the result of the expression 15-25/100+9*30%4? a, 82.25 b. 32.25 47 13 5. Which of the following will give the value -6.0: a. Math.ceil(-6.7) b. Math.abs(-6.7) c. Math.floor(-6.7) d, Math.round{(-6.7) 6. Which datatype cannot be used in a switch case construct? a. int b. char c. String d. double 7. Which of the following is not a relational operator? b.>= d.l= of the following is not an OOPs principle? a, Abstraction b. Polymorphism d. Inheritance c. Identifier 9, What will the value of x be after x= ++a+a~+at; ifa=5 before evaluation? a5 b.16 <17 4.18 10, Which is not a valid datatype? a, integer b. short c. long d. double Revision & 11. 12. 13. 14, 15. 16. 17. 18. 19. 22. 23. The blueprint that defines the variables and the methods common to all ofa certain kind is termed as, a. class ©. package b. object d, method A data type which contains integer as well as fractional part and occupies 32 bits space is a. float Becher ¢. double d. byte What is the final value stored in variable x? double x=Math.ceillMath.abs(-7.3)); azo b.8.0 c. 6.0 4.9.0 Which of the following keyword is used to create symbolic constants in Java? a. final b. Final ¢. Constant d. Const Name the type of error in the statement given below: double xy32; a, Logical error b. Syntax error ¢. Runtime error d. No error The keyword to create an object of a class i a. Create b. new c. New d. NEW The operator which acts on one operand is known as = a. binary b. ternary ‘¢. unary d. relational The ASCII code of 8! is a. 67 b. 66 c. 98 d.99 The jump statement that is used inside a switch case construct to terminate a statement sequence: a, continue b. break c. return d. goto Char is anon - primitive data type. a. True b. False Java start with /* and end with */. a. True b. False = is an assignment operator. a. True b. False Choose the odd one: a= b.% Gi cho ‘Touchpad Question Bank (CA 86)-X 24. Choose the odd one: a. double c. char 25. Choose the odd one: a. ifelse c. switch case 26. Choose the odd one: a. Robust , Inheritance 27. Give the output of the following: int x=2, y=4, 23= 1; int result = (++z)+y+(++x)+(z++); ait c. 10 28. Give the output of the following: int x; for ( x=1 ; x<=3; x++ Js System.out print (x); al23 a4 29. Give the output of the following: {it b. int d. String b. if d. while( ). b. Platform Independent , Multithreading b. 12 d.9 b.1234 de ("Google Meet"); eenee eon), exiioa(due — When x='g’ a. Google Meet b. Any software c. Google Meet, Any software d. WebEx 31. intv=5; while (-v>=0) f System.out.prit(v); } a, 43210 b. 54321 c. 543210 d, 4321 32, Read the following case study and answer the questions given below by choosing the correct option: Jay compilation is different from the compilation of other high-level languages. Other high“level languages use preter or compiler but in Java , the source code is first compiled to produce an intermediate code called the b. Joint Vision Mechanism d. Java virtual mechanism b. Only compiled d. Translated b. Byte code d. Machine code b. Compiler d. High level language 4c Sia 6.d a Bc Oe 14. a 15. b 3b dc 18. b 23.d 24.d dd 26. d 2a de b iv) a Name four principles of i a dew Gat ‘The four principles of O Abstraction ; Encapsulation Inheritance Polymorphism 2. Explain the use of the keyword continue in a loop. Ans. The keyword continue skips the rest of the statements of the current iteration and causes the next iteration. 3. What is meant by type casting? Ans. The forceful conversion of a variable from a predefined data type to another data type by the programmer is known as type casting. 4, Give 2 examples each of Relational and Logical operators. Ans. Relational operators: Logical operators: 5. Differentiate between = and = =. Ans. = isan assignment operator and it is used to assign a value to a variable. = = isa relational operator and it is used to check if 2 values are equal or not. 6. What do you understand by precedence of operators? Explain with an example. ‘Ans. Precedence of operators is the order in which the operators in an expression is evaluated. Eg.,x=5+6*2/3 Answer:x=9 Here, first 6*2 is evaluated, then 12/3 and finally 5+4. 7. Give two differences between float and double data types. Stores single precision numbers _ _Stores double precision numbers Takes up 4 Bytes of memory Takes up 8 Bytes of memory 8. What are variables? What do you mean by the scope of a variable? ‘Ans. Variables are named memory locations that store values of specific data types. The scope of a variable is the program region where it is accessible, that is the block in which it is declared, modified, and can be used. ‘9. Whats the function of the default statement in switch construct? Is it essential? ‘Ans. The default statement in a switch construct is executed when no matching case is found. No, itis not essential. 10, State one similarity and one difference between for loops and while loops. ‘Ans. Similarity: Both for loops and while loops are entry-controlled loops. Difference: for loops are generally used for known number of iterations and while loops are used for unknown number of iterations. 1. Akeyword that is used to declare a constant. Ans. final 2. Akeyword that prevents fall-through in a switch construct. ‘Ans. break Revision 3. The code generated when a Java program is compiled. ‘Ans. bytecode 4. The smallest individual unit ina Java program. Ans. Token 5. The character set used in Java. Ans. UNICODE 6. Amachine instruction for the Java interpreter. Ans. Bytecode | 7. Reserved words in Java, Ans. keywords 8. An interpreter that converts byte code to machine language. Ans. Java Virtual Machine 1. intx = 10,y = 15; X= ((xsy) 2 (y+) : (y-x)); What will be the value of x after executing the above statements? 25 9 2noljer" ‘Touchpa S+—q* g++ 10;ifx = 5andq = 2 before evaluation? while (++i<6) Pe System. out.print1n ( ‘Ans. x= 60 athe EMH) =6 2, int ctr=1; for (int i=l; ise5 ; i++) for (int j=l ; 3<=5 ; j+=2) Erctr?, System. out.print (ctr) ; Ans. 16 3. for(int x = 15; x>1; x - = 2) { if (x10) System. out.print1n(++x); else System.out.printin(--x); 0, y=37 a int z=x/y; double a=x/y; as"4a); nezt"\t System.out.print Ans.2=3 a=3.0 Milirrograms // 1. Using the switch statement, write a menu driven program to find the sum of the following series upto nt, () 12143214... ia x xe fi) + - 0) uw 2! ole An appropriate message should be printed for an incorrect choice. import java.util.Scanner; class SumSeries public static void main() Scanner sc=new Scanner (System. in); System.out.printin("1. Series 1"); System. out.printin("2. Series 2"); System.out.print1n("Enter your choice"); int ch=sc.nextInt(); System.out.print1n("Enter number of terms"); int n=sc.nextInt (); switch (ch) for(int i=1;i<=n;i++) = { e £*=i; sum2+= (Math.pow (x, i) /f) *sgn7 sgn*=-1; ) System. out .print1n("Sum="+sum2) ; break; default: System.out.print1n ("Wrong choice"); lowing as per the user’s choice: sible by 7 or has 7 as its Using a switch statement, write a menu-driven program to do the fol {i) Enter a number, check and print if itis a Buzz number or not. A Buzz number is div last digit. (ii) Enter a number and a single digit. Check and print if the digit is present in the number or not. For an incorrect choice, an appropriate error message should be printed. Ans. import java.util.Scanner; class Menu “public static void main() io ystem.out.printin("1. Buzz number"); ttem.out.printin("2. Search digit"); | is a Buzz number"); case 2: system.out.print1n( pnter a number")? int num=sc.nextInt ()7 System.out.printin("Enter a digit")? int d=sc.nextInt()? int flag=0; while (num!=0) { if( um810) { flag: brea’ num/=10; System.out.println(dt" is present in the number"); } else { System.out.printin(d+" is not present in the number"); } break; defaul ) : System.out.print1n ("Wrong input"); 1, Students puzzled over the different types of operators, like generally being confused between 2, Due toa lack of practice, students also make mistakes while using the keywords. Solution. The only solution to avoid such mistakes is to practice. Don't use bad variable names; always pick variable names that relate to the program code. £28 Touchpad Question Bank (CA 86)-X © A. Tick ( v) the correct opti © 1. Which of the following property of java is used to breaking up the program into smaller components? a, Encapsulation b. Modularity c. Inheritance d. Abstraction 2. Select the correct sequence of operator precedence in java. Bane, bi (), ay & 35000) { dise=5/100.0*price; price-=disc;} return disc; } void display() System.out.print1n("Company name: "+company) ; System.out.print1n("Model number: "+Modelno) ; System.out.print1n("Discount: "+calc()); System.out.printin("Price: "+price);} ic static void main() ith different keywords like this, new, etc. to make your ie Class as Basis of all Computation co Chapter Test-2 A. Tick ( v) the correct option. 1. Which of the following statement is correct regarding class? 2. Aclass is a blueprint of a set of objects having similar characteristics and behavior. b. Aclassis an object. . The keyword new is used to create a class. 4d. The dot operator(,) is used to refer to class, » What is the main use of new keyword in java? a. Ibis Used to refer to an instance. Iti used [to create an object ofa class. b. new d. this ing terms is defined as a group of related classes? b, this d. package ba 4.6 ee ae Wes a

You might also like