<<

Class 9 Chapter 4: Values and Data Types Write answers Q1. What is ? A character set is a set of , letters and some special characters that are valid in Java language. Q2. What are keywords? A keyword is a reserved word that have a special significance to the compiler and cannot be used anywhere else other than what it is intended for. Q3. What are Identifiers? State the rules while using Identifiers. Identifiers are the names of variables, methods, classes, packages and interfaces. While using identifiers the following set of rules are to be kept in mind. 1. It can have any (capital or small), digits, and dollar sign characters. For example, a, b, cat, mat123, cost_price, Topaz$ are all example of valid identifier 2. It should not begin with digits or should not contain any special character. For example 2ab, ab#, top@, etc, are invalid identifiers as it either begins with a digit or contain special characters (like #, @). 3. It cannot have a space between it. For example, simple interest or selling price are invalid identifiers as it contains a space. 4. It must not be a keyword. For example, for, while, do are invalid identifiers as they are keywords and are assigned some special function for the language. 5. It can be of any length. Even though Java gives you the flexibility to provide a huge length for an identifier a very long name is impractical and difficult to manage. Q4. What is a literal? What are the different types of literals available in Java? Literal is a constant value that can be assigned to a variable. The different types of literals in Java are: 1. -literal or Fixed point-literal 2. Floating point-literal 3. Boolean-literal 4. Character-literal 5. String-literal Q5. What is ? Unicode is a standard encoding system created by Unicode consortium that is used to encode a character in any computer language. Unicode uses 16 (2 ) to represent a character. ADVANTAGES OF UNICODE . It is the universal coding scheme. . Supports uniform coding width for all characters. . Character are unique for each character. Q6. What is ASCII ? ASCII stands for American Standard Code for Interchange. It uses 8 bits (1 ) to represent a character. ASCII code of White space blank is 32. Capital letters (A to Z) are 65 to 90 and Small letters (a to z) are 97 to 122. Q7. Difference between UNICODE and ASCII.

UNICODE ASCII It is a generalized form of coding It is a specific coding scheme used for limited scheme for numerous characters of number of characters. different scripts.

It represents higher range of codes. It represents limited range of codes.

Q8. What do you know about ESCAPE ? They are the non-graphic characters, used as commands to direct the cursor while printing. Escape sequences always begin with a backslash (\) character. Following are some of the escape sequences: . \t for Horizontal tab . \v for Vertical tab . \n for New line . \’ for Single quote . \” for Double quote . \b for Backspace . \f for Form feed . \r for . \0 for Null . \\ for Backslash

Q 9. What do you mean by ? Data types are used to identify the type of data a memory location can hold and the associated operations of handling it. Q 10. Define variable with an example. A variable represents a memory location through a symbolic name which holds a known or unknown value of a particular data type. This name of the variable is used in the program to refer to the stored value. Example: int mathScore = 95; Q 11. What is the use of keyword ‘final’? Explain with an example. The keyword final before a variable declaration makes it a constant. Its value can't be changed in any circumstances in the program. Example: final int DAYS_IN_A_WEEK = 7; Q 12. State two kinds of data types. Two kinds of data types are: 1. Primitive Datatypes. 2. Non-Primitive Datatypes. Q 13. What do you understand by Token? Name different types of tokens. A token is the smallest element of a program that is meaningful to the compiler. The different types of tokens in Java are: . Literals or Constants: These are the fixed values that do not change during program execution. . Identifiers: These are the variables used in a Java program. . Assignments: The = sign is an assignment as it helps to store values in a variable. . Punctuators: Special characters like (?), dot (.), semicolon (;) are punctuators. . Separators: Special characters used to separate various parts of a Java program are separators. . Operators: These are symbols that perform some calculation or operation with values. . Keywords: These are reserved words which have special meaning for the compiler. Q 14. What are the rules to assign a variable in a Java programming? 1. Name of the variable should be a of alphabets, digits, underscore and dollar sign characters only. 2. It should not start with a digit. 3. It should not be a keyword or a boolean or literal. Q 15. Explain the term 'type casting'? The process of converting one predefined type into another is called type casting. Q 16. Perform the following: (a) Assign the value of pie (3.142) to a variable with the requisite data type. double pi = 3.142d; (b) Assign the value of (1.732) to a variable with the requisite data type. double x = 1.732d; Q 17. Distinguish between:

(a) Integer and floating constant Integer Constant Floating Constant Integer Constants represent whole number Floating Constants represent fractional numbers values like 2, -16, 18246, 24041973, etc. like 3.14159, -14.08, 42.0, 675.238, etc.

Integer Constants are assigned to variables Floating Constants are assigned to variables of of data type — byte, short, int, long, char data type — float, double (b) Token and Identifier Token Identifier

A token is the smallest element of a Identifiers are used to name things like classes, program that is meaningful to the objects, variables, arrays, functions an so on. compiler.

(c) Character and String constant Character Constant String Constant Character Constants are written by String Constants are written by enclosing a set enclosing a character within a pair of of characters within a pair of double quotes. single quotes. Character Constants are assigned to String Constants are assigned to variables of variables of type char. type String. () Character and Boolean literal Boolean Literal Character literals are written by enclosing A boolean literal can take only one of the two Boolean a character within a pair of single quotes. values represented by the words true or false. Character literals can be assigned to Boolean literals can only be assigned to variables variables of any numeric data type — declared as boolean byte, short, int, long, float, double, char Escape Sequences can be used to write Only true and false values are allowed for boolean character literals literals

Q 18. Write down the data type of the following: (a) Integer int (b) Long Integer long (c) A fractional number double (d) A special character char Q 19. What do you understand by Boolean type data? Explain with an example. A is used to store one of the two boolean values — true or false. The size of boolean data type is 8 bits or 1 byte. Example: boolean bTest = false; Q 20. What do you understand by ? Give two examples. Primitive data types are the or fundamental data types used to declare a variable. Examples of primitive data types in Java : . byte (1 byte) . short (2 bytes) . char (2 bytes) . int (4 bytes) . long (8 bytes) . float (4 bytes) . double (8 bytes) . boolean (1 byte) Q 21. Why is it necessary to define data type in Java programming? Data types tells Java how much memory it should reserve for storing the value. Data types also help in preventing errors as the compiler can check and flag illegal operations at compile time itself. Q 22. Define the following with an example each: (a) Implicit In implicit type conversion, the result of a mixed mode expression is obtained in the higher most data type of the variables without any intervention by the user. Example: int a = 10; float b = 25.5f, c; c = a + b; (b) Explicit type conversion In explicit type conversion, the data gets converted to a type as specified by the programmer. For example: int a = 10; double b = 25.5; float c = (float)(a + b); Q 23. Define 'Coercion' or Type Promotion with reference to type conversion. In a mixed-mode expression, the process of promoting a data type into its higher most data type available in the expression without any intervention by the user is known as Coercion. Example: byte b = 42; int i = 50000; double result = b + i; Q 24. What do you mean by type conversion? How is implicit conversion different from explicit conversion? The process of converting one predefined type into another is called type conversion. In an implicit conversion, the result of a mixed mode expression is obtained in the higher most data type of the variables without any intervention by the user. For example: int a = 10; float b = 25.5f, c; c = a + b; In case of explicit type conversion, the data gets converted to a type as specified by the programmer. For example: int a = 10; double b = 25.5; float c = (float)(a + b); Q 25. In what way is static declaration different from dynamic declaration? In static initialization, the initial value of the variable is provided as a literal at the time of declaration. For example: int mathScore = 100; double p = 1.4142135; char ch = 'A'; In dynamic initialization, the initial value of the variable is the result of an expression or the return value of a method call. Dynamic initialization happens at runtime. For example: double x = 3.14159, y = 1.4142135; double z = x + y; Q 26. What do you mean by non-primitive data type? Give examples. A non-primitive data type is one that is derived from Primitive data types. A number of primitive data types are used together to represent a non-primitive data type. Examples of non-primitive data types in Java are Class and Array. Q 27. Predict the return data type of the following: (i) int p; double q; r = p+q; System.out.println(r); Answer Return data type is double. (ii) float m; p = m/3*(Math.pow(4,3)); System.out.println(p); Answer Return data type is double.

Q 28. What are the resultant data types if the following implicit conversions are performed? Show the result with flow lines. int i; float f; double d; char c; byte b; (a) i + c/b; Answer i + c/b; ⇒ int + char / byte ⇒ int + char ⇒ int (b) f/d + c*f; Answer f/d + c*f; ⇒ float / double + char * float ⇒ double + float ⇒ double (c) i + f - b*c; Answer i + f - b*c; ⇒ int + float - byte * char ⇒ int + float - char ⇒ float - char ⇒ float (d) (f/i)*c + b; Answer (f/i)*c + b; ⇒ (float / int) * char + byte ⇒ float * char + byte ⇒ float + byte ⇒ float (e) i + f- c + b/d; Answer i + f- c + b/d; ⇒ int + float - char + byte / double ⇒ int + float - char + double ⇒ float - char + double ⇒ float + double ⇒ double (f) i/c + f/b; Answer i/c + f/b ⇒ int / char + float / byte ⇒ int + float ⇒ float

Multiple Choice Questions Q 1 A constant which gives the exact representation of data is called 1. Variable 2. Literal 3. Identifier 4. Character Q 2 A word used in a high level language which has a special meaning attached to it is called 1. Class 2. Identifier 3. Keyword 4. Literal Q 3 A character literal is assigned to a: 1. Char variable 2. Char type literal 3. String variable 4. String literal Q 4 A character literal is enclosed in: 1. ' ' ⇐ Answer 2. " " 3. : : 4. { } Q 5 A set of characters is assigned to: 1. String variable 2. Static variable 3. Boolean variable 4. None Q 6 The ASCII codes of upper case alphabets range from: 1. 65 - 90 2. 60 - 85 3. 65 - 91 4. 97 - 122 Q 7 Which of the following results in integer type? 1. 11.4F/3.2D 2. 13.8F/4.6F; 3. 12/3 4. none Q 8 Which of the following is non-primitive data? 1. char 2. long 3. object 4. short Q 9 Which of the following type is an exact representation of fractional values? 1. char 2. double 3. byte 4. String Q 10 Boolean Data is used to test a particular condition i.e. true or false. Which of the following is a correct representation? a. boolean m=true b. boolean m='true' c. boolean m="true" d. none

Fill in the blanks 1. The character sets of Java is like alphabets of English language. 2. A standard encoding system way of representing characters is Unicode. 3. ASCII code is number to represent a character. 4. Each individual component of a Java statement is known as token. 5. In Java, the constants are also called literals. 6. Assignment operator is used to store a value in the variable. 7. The , exclamation, question mark etc., are termed as Separators in Java language. 8. An element of Java program that is used to identify a class, function or value is called as identifier. 9. Integer type value occupies 4 bytes in the memory. 10. A Java expression that contains all the elements of same data type is pure expression. 11. Character Set is a valid set of characters used in a Java language. 12. A unicode is a type encoding that can represent characters of all the languages of the world. 13. Token is the fundamental building of a program. 14. Keywords are reserved words in Java that provide a special meaning to the java compiler. 15. The names given to different parts of a program are called Identifiers. 16. integer literals are always prefixed with 0. 17. Literals that are enclosed within double quotes are called String literals. 18. Symbolic constant declarations are always preceded with the final keyword. 19. Operators are special symbols that perform specific operations on one or more operands and then return a result. 20. The true and false literals are both boolean literals.

Very short answer type questions 1. Identify the mistake in each of these lines and correct it: a. int goto=5; Ans. The identifier goto is invalid, as it is a keyword. Correction: int g=5; b. float a=12.3; Ans. The float literals should be suffixed with ‘f’. Correction: float a=12.3f; c. doubles b=15; Ans. The data type is wrongly spelled. Correction: double b=15; d. long b=1536458888397632; Ans. The long literal should be suffixed with an ‘L’. Correction: long b=1536458888397632L; e. String s= ‘Computer’; Ans. String literals should be enclosed in double quotes Correction: String s= “Computer”; 2. State with reasons why the following variable names are invalid: a. for Ans. Invalid, as for is a keyword. b. book list Ans. Invalid, as a space cannot be present in a variable name. c. 2ndTerm Ans. Invalid, as a variable name cannot begin with a digit. d. hash# Ans. Invalid, as a variable name cannot have a special character like #. e. sick@always Ans. Invalid, as a variable name cannot have a special character like @. 3.What is the resultant data type of the following mathematical expression? a+b*c-d a. where a is int, b is int, c is float and d is float type Ans. float b. where a is float, b is long, c and d are of int type Ans. float c. where a is of double and b,c and d are of int type. Ans. double d. where a is char and b,c and d are of int type Ans. int. e. where a, b, c and d are of int type, however the expression is slightly modified as (a+b*c- d)/7.0 Ans. double Short answer type questions State with reasons why the following initializations are incorrect: a. int a=5; short b=a; Ans. The short b=a; is invalid as an int cannot be initialised to a short type variable. b. double a=5.3; float b=a; Ans. The float b=a; is invalid as a which is of double type cannot be initialised to a variable of float type. c. int a=01238; Ans. The int a =01238; is invalid as an starting with a 0 is considered to be of octal data type and therefore cannot have a digit 8. d. float a=17.36f; int b=a; Ans. The int b=a; is invalid as a is of float type, that cannot be initialised to a variable b of int type. e. boolean a=true; int b=a; Ans. The int b=a; is invalid as the assignment is of incompatible type.