JAVA UNIT-1 2MARKS
JAVA UNIT-1 2MARKS
☛Token means the smallest individual units of program. In java the following tokenare available
1. Comments
2. Identifiers
3. Separators
4. literals
5. Keywords
Q3. What is type casting in Java?
☛Type casting is the process of converting the one data type of a variable intoanother data type. In
java, there are two types of type casting
1.Implicit (or) Widening Type Casting
2.Explicit (or) Narrow Type Casting
Syntax: <datatype> variableName = (<datatype>) value;
☛ A lower data type is transformed into a higher one by a process known as widening type casting.
Implicit type casting and casting down are some names for it.
Syntax: larger_data_type variable_name = smaller_data_type_variable;
☛ Variable is like a container which can hold some values. Variables are locations inthe memory that
can hold values. Java has three kind of variables.
Instance variables
Local variables
Class variables
☛ Local variable is a variable declared inside a method body, block or constructor. These variables are
created when the method, constructor, or block is entered and are destroyed once it exits. Local
variables are only accessible within the scope theyare declared in and cannot be accessed outside of
that scope. It means variable is only accessible inside the method block or constructor that declared it.
Q8. What is a Class variable?
Class variable is a variable defined in a class but outside of any class methodsand shared by all
instances of that class.
Class variables are declared using the static keyword.
Q9. What is instance variable?
☛ Instance variables are non-static variables and are declared in a class outside of any method,
constructor, or block.
As instance variables are declared in a class, these variables are created whenan object of the
class is created and destroyed when the object is destroyed.
Q10. How to take input from user in JAVA?
☛Java Scanner class allows the user to take input from the console. It belongs to java.util package. It is
used to read the input of primitive types like int, double, long, short, float, and byte. It is the easiest way
to read input in Java program.
Syntax:
Scanner objectname=new Scanner(System.in);
☛Command line arguments in Java are parameters passed to the program at the time of execution.
These arguments are received in the main() method as a string array and can be used as input within
the program.
Q12. What are escape sequences in java?
Escape sequences in Java are special characters preceded by a backslash (\) that havea specific
meaning to the compiler. They are used to represent characters that are otherwise difficult to include in
a string, such as newlines, tabs, or quotation marks.
There are eight primary escape sequences in Java, each serving a unique purpose. Common Escape
Sequences
Here are some of the most commonly used escape sequences in Java:
1. \t: Inserts a tab in the text. System.out.println("Hello\tWorld!");Output: Hello World!
2. \b: Inserts a backspace in the text. System.out.println("Hello\bWorld!");Output: HellWorld!
3. \n: Inserts a newline in the text. System.out.println("Hello\nWorld!");Output: //Hello // World!
4. \r: Inserts a carriage return in the text. System.out.println("Hello\rWorld!");Output: World!
5. \f: Inserts a form feed in the text. System.out.println("Hello\fWorld!");Output: Hello // World!
6. \': Inserts a single quote character in the text. System.out.println("Hello\'World!");Output:
Hello'World!
7. \": Inseíts a double quote chaíacteí in the text.
8. \\: Inseíts a backslash chaíacteí in the text.
☛Data type defines the values that a variable can take, for example if a variable has int data type, it can
only take integer values. In java we have two categories of data type:
☛Primitive types in Java are predefined and built into the language. In Java, we have eight primitive
data types: boolean, char, byte, short, int, long, float and double.
byte, short, int and long data types are used for storing whole numbers.
boolean data type is used for variables that holds either true or false.
Q15. What are non-primitive datatypes in java?
☛Non-primitive data types are called reference types because they refer to objects. Non-primitive types
can be used to call methods to perform certain operationsExamples of non-primitive types are Strings,
Arrays, Classes etc.
☛The variable's scope refers to the region where they are created and accessed in a given program or
function. The variable scope also refers to its lifetime.
In Java, all identifiers are statically scoped, i.e.scope of a variable can be determinedat compile time
and independent of function call stack.
Q17. What is symbolic constant in java?
☛In Java, a symbolic constant is a named constant value defined once and used
throughout a program. Symbolic constants are declared using the final keyword.
Which indicates that the value cannot be changed once it is initialized.
The naming convention for symbolic constants is to use all capital letters withunderscores
separating words.
Syntax : final data_type CONSTANT_NAME = value;
Q18. Define formatted output with printf() method?
☛In Java, the printf() method is used to format output in a way similar to
the printf() function in C. This method is part of the java.io.PrintStream class and allows you to create
formatted strings using various format specifiers.
Syntax:
System.out.printf("Hello, %s!%n", "World");Output:
Hello, World!
Q19. What is static variable in java?
☛In Java, static variables are variables that belong to the class rather than any specific instance of the
class. This means that there is only one copy of the static variable, regardless of how many instances of
the class are created. Static variablesare also known as class variables.
Q20. What is static methods in java?
☛A static method in Java is a method that is part of a class rather than an instance of thatclass
☛Operators are used to perform operations on variables and values. Operatorsmake tasks like addition,
multiplication, etc
Types of Operators in Java
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
Q23.Define control statement?
☛The statements that control the execution flow of the program are known as control statements.