Class 9 Computer Chapter 4: Values and Data Types Write Answers Q1
Total Page:16
File Type:pdf, Size:1020Kb
Class 9 Computer Chapter 4: Values and Data Types Write answers Q1. What is Character Set? A character set is a set of alphabets, 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 alphabet (capital or small), digits, underscore 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#c, 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. Integer-literal or Fixed point-literal 2. Floating point-literal 3. Boolean-literal 4. Character-literal 5. String-literal Q5. What is Unicode? Unicode is a standard encoding system created by Unicode consortium that is used to encode a character in any computer language. Unicode uses 16 bits (2 bytes) to represent a character. ADVANTAGES OF UNICODE . It is the universal coding scheme. Supports uniform coding width for all characters. Character codes are unique for each character. Q6. What is ASCII code? ASCII stands for American Standard Code for Information Interchange. It uses 8 bits (1 byte) 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 SEQUENCES? 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 Carriage return . \0 for Null . \\ for Backslash Q 9. What do you mean by data type? 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 question mark (?), 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 sequence 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 null 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. (d) Character and Boolean literal Character 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 boolean data type 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 primitive data type? Give two examples. Primitive data types are the basic 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 type conversion 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.