Variables
Variables
Variables
What is a variable ?
•Every variable is assigned a data type which designates the type and
quantity of value it can hold.
int count;
Variable Initialization
Container
To initialize a variable, you must assign it a valid value. named “count”
holding a value
100
count=100;
100
count
You can combine variable declaration and initialization.
int count=100;
NAMING CONVENTION OF VARIABLES
a) ASCII Code
b)Unicode
c) Both (a) & (b)
d) None of these
Predict the output
class mainclass
{ a) 0
public static void main(String args[])
{ b) 1
boolean var1 = true;
c) true
boolean var2 = false;
if (var1) d) false
System.out.println(var1);
else
System.out.println(var2);
}
}
Predict the output
class booloperators a) 0
{
b) 1
public static void main(String args[])
{ c) true
boolean var1 = true;
boolean var2 = false; d) false
System.out.println((var1 & var2));
}
}
Predict the output
class asciicodes
a) 162
{
public static void main(String args[]) b) 65 97
{ c) 67 95
char var1 = 'A';
d) 66 98
char var2 = 'a';
System.out.println((int)var1 + " " + (int)var2);
}
}
Predict the output
class A{ A. 258 325 325
public static void main(String args[]){ B. 258 326 326
byte b;
C. 2 325 69
int i = 258;
double d = 325.59; D. Error
b = (byte) i;
System.out.print(b);
i = (int) d;
System.out.print(i);
b = (byte) d;
System.out.print(b);
}}
class A
Predict the output
{ A. 10 20 10 100
public static void main(String args[])
B. 10 20 10 20
{
int x; C. 10 20 10 10
x = 10;
if(x == 10) D. Error
{
int y = 20;
System.out.print("x and y: "+ x + " " + y);
y = x*2;
}
y = 100;
System.out.print("x and y: " + x + " " + y);
}
}
Predict the output
public class Test
{
static void test(float x) A. float
{
System.out.print("float"); B. double
}
static void test(double x) C. Compilation Error
{
System.out.print("double"); D. Exception is thrown at runtime
}
public static void main(String[] args)
{
test(99.9);
}
}
Predict the output
public class Test
A. 8 7
{
B. 10 7
public static void main(String[] args)
C. Compilation fails with an error
{
at line 3
int i = 010;
D. Compilation fails with an error
int j = 07; at line 5
System.out.println(i); E. None of these
System.out.println(j);
}
}
Predict the output
public class MyClass
(A) 10
{
public static void main(String[] args) (B) 11
{ (C) 12
int a = 10;
(D) Compilation Error
System.out.println(++a++);
}
}
Predict the output
public class Main
(A) 138
{
public static void main(String[] args) (B) 264
{ (C) 41
int a = 5+5*2+2*2+(2*3);
(D) 25
System.out.println(a);
}
}
Predict the output