Questions on variables in java with answers
(1)
public class Test {
public static void main(String[] args) {
int a=5;
{
Integer b=10;
}
int c=a+b;
System.out.println(c);
}
}
What will output when you compile and run the above code?
(a)15
(b)5
(c)Compiler error
(d) Run time error
Answer: (c)
Explanation:
Scope of the any variable in a function is the braces in which it has been declared. Variable a has been declared within main function braces. So its scope is whole the main function while variable b has been declared in another braces so its scope and visibility is only that block or braces. After that block variable a is visible while variable b has dead.
So, It is error to write int c=a+b;
(2)
public class Loop {
public static void main(String[] args) {
for(int i=-0;i<3;i++){
System.out.print(i);
}
System.out.print(i);
}
}
What will output when you compile and run the above code?
(a) 0 1 2 3
(b) 0 1 2 2
(c) 0 0 1 2
(d) Compiler error
Answer: (d)
Explanation:
Scope of the any variable is in a function or loop or the only braces in which it has been declared. Variable i has been declared within for loop. So it is not visible to outside the for loop
Identifier
(3)
public class Identifier {
public static void main(String[] args) {
int a$=5;
Integer b$=10;
int _c=a$+b$;
System.out.print(_c);
}
}
What will output when you compile and run the above code?
(a) 15
(b) 50
(c) Compile time error
(d) Run time error
Answer: (a)
Explanation:
_c=5+10=15.
In java variable name can be start with underscore (_) and it can contain special character dollar singe ($)
(4)
public class Identifier {
public static void main(String[] args) {
int $=5;
final byte $$=-10;
int _$=$+$$;
System.out.print(_$);
}
}
What will output when you compile and run the above code?
(a)-5
(b) 5
(c) 105
(d) Error
Answer: (a)
Explanation:
-$=5+ (-10) =-5
In java programming language variable name includes alphabet, digits few special characters like underscore (_), dollar singe ($).
(5)
public class Identifier {
public static void main(String[] args) {
int _=5;
short __ =2;
int ___=__-~_;
System.out.print(___);
}
}
What will output when you compile and run the above code?
(a) 2
(b) 4
(c) 8
(d)Error
Answer: (c)
Explanation:
In java programming language variable name includes alphabet, digits few special characters like underscore (_), dollar singe ($).
Here _ (one underscore), __ (two underscore) or ___ (three underscore) are valid name of any variable.
So, ___=2+~5=2-(-6) =2+6=8
Note: ~ is 1’s complementing operator.
Java questions of data types and answer
Java questions on if else statements
Java questions on switch case and answers
Java questions on looping and answers
Java questions on characters
Java questions on strings and answers
Java questions on variables and answers
Java questions on automatic type promotions
Java questions on bit wise operator
Java questions on operators and answers
Java questions on static keyword and answers
Java questions on super keyword and answers
Java questions on abstract class an answers
Java questions on interface and answers
Java questions on enum data type and answers
Java questions on break and continue keyword
Java questions of primitive data types
Java questions on conditional operators
Java questions on two dimensional array and answers
Java questions of data types and answer
Java questions on if else statements
Java questions on switch case and answers
Java questions on looping and answers
Java questions on characters
Java questions on strings and answers
Java questions on variables and answers
Java questions on automatic type promotions
Java questions on bit wise operator
Java questions on operators and answers
Java questions on static keyword and answers
Java questions on super keyword and answers
Java questions on abstract class an answers
Java questions on interface and answers
Java questions on enum data type and answers
Java questions on break and continue keyword
Java questions of primitive data types
Java questions on conditional operators
Java questions on two dimensional array and answers
No comments:
Post a Comment