Variables, Expressions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Variables, Expressions

Multiple choice questions 7. A variable name should never begin with.


(a) A number
1. Where do we declare local variables ? (b) Special characters but underscore and dollar
(a) 
It declares inside the class and outside of sign
method. (c) All of the above
(b) I t declares inside the class with static prefix 8. Choose a valid variable name.
and outside of method.
(a) 5thTeam (b) &5thteam
(c) It declares inside the method.
(c) _5thTeam (d) None of the Above
(d) none of these
9. Which variable names are the invalid variable
2. Where do we declare instance variables ? names ?
(a) 
It declares inside the class and outside of (a) 1stLevel (b) first Level
method.
(c) *firstLevel (d) All of the Above
(b) I t declares inside the class with static prefix
10. Which variable names are the valid variable
and outside of method.
names ?
(c) It declares inside the method.
(a) $1stLevel (b) _1stLevel
(d) none of these
(c) £1stLevel (d) All of the Above
3. Where do we declare class variables ?
11. The two possible Logical Operator types are:
(a) 
It declares inside the class and outside of
(a) Bitwise Logical (b) Boolean Logical
method.
(c) Arithmetic Logical (d) (a) and (b)
(b) I t declares inside the class with static prefix
12. Which data type do we use with Boolean logical
and outside of method.
operators in Java ?
(c) It declares inside the method.
(a) true/false boolean data
(d) none of these
(b) 1 and 0 of individual Bits
4. Which element can not be used as variable name
(c) characters of a String
in Java programming language ?
(d) None of the above
(a) literal
13. Which type of data can be used with Bitwise
(b) Keywords
logical operators in Java?
(c) Identifier
(a) true/false boolean data
(d) Identifier and Keywords
(b) 0 and 1 individual bits of data
5. Name of variable is called:
(c) Characters of a String
(a) Data Type
(d) None of the above
(b) Constant
14. Bitwise logical operators are also called _______.
(c) Identifier
(a) Logical operators (b) Bitwise operators
(d) None of the above
(c) Binary operators (d) None of the above
6. Which variable name is invalid variable name ?
15. Input used for Logical Operators are:
(a) tenthTeam (b) TenthTeam
(a) 1 and 0 (b) true / false
(c) 10thTeam (d) None of the Above
(c) char / String (d) None of the above
16. Output given by any Logical operation in Java: 27. Why Short Circuit AND (&&) and Short Circuit
(a) 1 or 0 (b) true or false OR (||) operators are fast in Java?
(c) char or String (d) None of the above (a) B
 y skipping the second expression or operand
17. Which Logical operator works with a Single if possible and save time.
Operand? (b) By using extra memory on the machine
(a) Logical AND (b) Logical OR (c) By using extra CPU processing power
(c) Logical Exclusive OR (d) Logical NOT (d) None of the above
18. Which is a Logical Unary NOT operator in Java? 28. Which operators are involved in Arithmetic
(a) ~ (b) ! expression in Java ?
(c) # (d) ^ (a) Addition (+), Subtraction (-)
19. What will be the output of a Logical OR (|) (b) Multiplication (*), Division (/)
operation if one of the inputs/operands is false? (c) 
Modulo Division (%), Increment/Decrement
(a) false (b) true (++/--), Unary Minus (-), Unary Plus (+)
(c) true or false (d) None of the above (d) All of the above
20. What will be the output of Logical AND (&) 29. Which is the correct Compound Assignment
operation if one of the inputs/operands is false? Arithmetic Operators in Java .
(a) false (b) true (a) +=, -= (b) *=, /=
(c) true or false (d) None of the above (c) %= (d) All of the above
21. What will be the output for a Logical OR (|) 30. Choose the correct output of Java code snippet?
operation when inputs/operands is true? int a = 2 - - 7;
(a) false (b) true System.out.println(a);
(c) true or false (d) None of the above (a) –5 (b) 10
22. What will be the output of a Logical AND (&) (c) 9 (d) Compiler Error
operation when inputs/operands is true?
31. Choose the correct output of Java code snippet ?
(a) false (b) true short p = 1;
(c) true or false (d) None of the above short k = p + 2;
23. What will be the output of a Logical AND (&) System.out.println(k);
operation if both inputs/operands are true?
(a) 1 (b) 2
(a) false (b) true
(c) 3 (d) Compiler error
(c) true or false (d) None of the above
32. The arithmetic operator in Java that gives the
24. What will be the output of a Logical OR (|) Remainder of Division is :
operation if both the inputs/operands are true?
(a) / (b) @
(a) true (b) false
(c) % (d) &
(c) true or false (d) None of the above
33. What type of associativity do Arithmetic operators
25. Which operator is fast AND (&) and Short Circuit +, -, /, * and % have?
AND(&&) operators in Java?
(a) Right to Left (b) Left to Right
(a) AND operator
(c) Right to Right (d) Left to Left
(b) Short Circuit AND
34. Which of the following operators have more
(c) Both work at the same speed priority?
(d) None of the above (a) P
 ostfix operators have more priority than
26. Which operator is fast OR(|) and Short Circuit Prefix operators
OR (||) operators in Java? (b) P
 refix operators have more priority than
(a) OR Operator Postfix operators
(b) Short Circuit OR operator (c) B
 oth Prefix and Postfix operators have equal
(c) Both work at the same speed priority
(d) None of the above (d) None of the above
35. Which of the following operator has less priority? 42. _________ is the order of precedence (highest to
(a) P
 ostfix Decrement has less priority than Prefix lowest) of following operators?
Increment 1. &
(b) P
 refix Increment has less priority than Postfix 2. ^
Decrement 3. ?:
(c) Both operators have same priority (a) 1 -> 2 -> 3 ==== (b) 2 -> 1 -> 3
(d) None of the above
(c) 3 -> 2 -> 1 (d) 2 -> 3 -> 1
36. How is the associativity used by Increment and
43. _________________ statement is incorrect?
Decrement arithmetic operators in Java ?
(a) Equal to operator has least precedence
(a) Left to Right (b) Right to Left
(b) Brackets () have highest precedence
(c) Left to Left (d) Right to Right
(c) 
Division operator, /, has higher precedence
37. Which of the following is the correct statement
about Java Operators +, -, *, / and %. than multiplication operator
(a) + and - have equal priority (d) Addition operator, +, and subtraction operator
(b) * and / have equal priority have equal precedence
(c) / and % have equal priority 44. ___________ returned by greater than, <, and
(d) All the above equal to, ==, operator?
38. Choose the group with higher priority in operator (a) Integers
groups (++, --) and (+, -, *, /, %) in Java. (b) Floating - point numbers
(a) (++, --) group has higher priority than (+, -, *, /, (c) Boolean
%) group (d) None of the mentioned
(b) (++, --) group has lower priority than (+, -, *, /, 45. ______________operators can operate on a
%) group
boolean variable?
(c) (++, --) group and (+, -, *, /, %) group have
1. &&
equal priority
2. ==
(d) None of the above
3. ?:
39. Operator with highest precedence:
4. +=
(a) () (b) ++
(c) * (d) >> (a) 3 & 2 (b) 1 & 4
(c) 1, 2 & 4 (d) 1, 2 & 3
Fill in the blanks 46. ___________ operators can skip evaluating right
hand operand.
40. ________should be expression1 evaluate to in
(a) ! (b) |
using ternary operator in the following line?
(c) & (d) &&
expression1 ? expression2 : expression3
47. __________ operator is having highest precedence.
(a) Integer
(a) () (b) ++
(b) Floating – point numbers
(c) * (d) >>
(c) Boolean
48. Expression1 should be _______ to evaluate using
(d) None of the above
ternary operator?
41. ________ is the value stored in x in the following
lines of Java code? expression1 ? expression2 : expression3
int x, y, z; (a) Integer
x = 0; (b) Floating – point numbers
y = 1; (c) Boolean
x = y = z = 8; (d) None of the mentioned
(a) 0 (b) 1 49. ________ is the value stored in x in following lines
(c) 9 (d) 8 of code?
int x, y, z; 3. False Part of (iii) \\
x = 0; conditional
y = 1; operator
x = y = z = 8; 4. Character escape (iv) Must return a
(a) 0 (b) 1 code not in java value
(c) 9 (d) 8
(a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
(b) 1-(i), 2-(ii), 3-(iv), 4-(iii)
Match the following
(c) 1-(ii), 2-(i), 3-(iii), 4-(iv)
50. Match the columns: (d) 1-(ii), 2-(iv), 3-(i), 4-(iii)
Group A Group B 53. Match the columns:
1. Operands of (i) Integers and Group A Group B
arithmetic floating - point
numbers 1. Portability and (i) Use of pointers
operators security of Java
2. Modulus operator, (ii) Numeric & 2. Not a Java features (ii) Byte code is
%, Characters executed by JVM
3. Operator, −−, (iii) ( ) , { } 3. \u0021 article (iii) JDB
decreases the 4. Find and fix bugs (iv) Unicode escape
value sequence
4. Highest order (iv) by 1 (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
precedence
(b) 1-(i), 2-(ii), 3-(iv), 4-(iii)
operator
(c) 1-(ii), 2-(i), 3-(iii), 4-(iv)
(a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
(d) 1-(ii), 2-(iv), 3-(i), 4-(iii)
(b) 1-(i), 2-(ii), 3-(iv), 4-(iii)
54. Match the columns:
(c) 1-(ii), 2-(i), 3-(iii), 4-(iv)
Group A Group B
(d) 1-(ii), 2-(iv), 3-(i), 4-(iii)
1. Return type of (i) 0xnf029L
51. Match the columns:
the hash Code()
Group A Group B
method
1. && and || (i) Ternary Operator 2. Valid long literal (ii) int
operators
3. Float a = 35 / 0 (iii) It has no class
2. Colon (?:) operator (ii) Combine two return? name
boolean values
4. Anonymous inner (iv) Infinity
3. Java Ternary (iii) true or false class
operator
(a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
4. Condition of (iv) Conditional
a Java Ternary Operator (b) 1-(i), 2-(ii), 3-(iv), 4-(iii)
operator (c) 1-(ii), 2-(i), 3-(iii), 4-(iv)
(a) 1-(ii), 2-(i), 3-(iv), 4-(iii) (d) 1-(ii), 2-(iv), 3-(i), 4-(iii)
(b) 1-(i), 2-(ii), 3-(iv), 4-(iii)
Programming based questions
(c) 1-(ii), 2-(i), 3-(iii), 4-(iv)
(d) 1-(ii), 2-(iv), 3-(i), 4-(iii) 55. Choose correct output of Java code snippet?
52. Match the columns: short k=1;
Group A Group B k += 2;
1. Assignment (i) must return a System.out.println(k);
operator value (a) 1
2. True part of (ii) Assignment and (b) 2
conditional Lambda operator (c) 3
operator (d) Compiler error about Type Casting
56. With reference to the program code given below, Natural Numbers is = “ + sum);
answer the questions that follow: }
public class ReverseNumber }
{
(a) What is the output of the following program?
public static void main(String[] args)
(i) Sum of First 100 Natural Numbers is = 5050
{
(ii) Compilation error
int number = 987654, reverse = 0;
(iii) 15
while(number != 0)
(iv) Runtime error
{
int remainder = number % 10; (b) What is the name of the class in the above
reverse = reverse * 10 + remainder; program ?

number = number/10; (i) Constructor


} (ii) Sum of Natural Number 2

System.out.println(“The reverse of the (iii) String[] args
given number is: “ + reverse); (iv) Name
} 58. Regarding the program code given below, answer
} the questions that follow:
(a) What is the output of the following program? public class CheckPositiveOrNegative
(i) The reverse of the given number is: 456789 {
(ii) Compilation error
public static void main(String[] args)
(iii) 15
{
(iv) Runtime error
//number to be check
(b) What is the name of the class in the above
program int num=912;
(i) Constructor //checks the number is greater than 0 or not
(ii) ReverseNumber if(num>0)
(iii) String[] args {
(iv) Name 
System.out.println(“The number is
57. Regarding the program code given below, answer positive.”);
the questions that follow: }
public class SumOfNaturalNumber2 //checks the number is less than 0 or not
{
else if(num<0)
public static void main(String[] args)
{
{

System.out.println(“The number is
int num = 100, i = 1, sum = 0;
negative.”);
//executes until the condition returns true
}
while(i<= num)
/ /executes when the above two conditions
{
return false
//adding the value of i into sum variable
else
sum = sum + i;
//increments the value of i by 1 {

i++; System.out.println(“The number is zero.”);


} }
//prints the sum }
System.out.println(“Sum of First 100 }
(a) What is the output of the following program? (b) What does the above program do?
(i) The number is positive. (i) Initialize a number
(ii) Check whether the number is positive or
(ii) The number is Negative.
negative
(iii) 15 (iii) Give odd or even number
(iv) Runtime error (iv) None of the above

Answers

Multiple choice questions 12. (a) true/false boolean data


13. (b) 0 and 1 individual bits of data
1. (c) It declares inside the method. 14. (b) Bitwise operators
Explanation: A variable declared inside the body
Explanation: Java defines several bitwise
of the method is called local variable. You can
operators, which can be applied to the integer
use this variable only within that method and
types, long, int, short, char, and byte.
the other methods in the class aren’t even aware
15. (b) true / false
that the variable exists. A local variable cannot be
defined with “static” keyword. 16. (b) true or false
2. (a) 
It declares inside the class and outside of 17. (d) Logical NOT
method. 18. (b) !
Explanation: Instance variables are declared in 19. (c) true or false
a class, but outside a method, constructor or any 20. (a) false
block. When space is allocated for an object in 21. (b) true
the heap, a slot for each instance variable value is
22. (c) true or false
created.
23. (b) true
3. (b) I t declares inside the class with static prefix
and outside of method. 24. (a) true

Explanation: Class variables also known as static 25. (b) Short Circuit AND
variables are declared with the static keyword Explanation: In Java logical operators, if the
in a class, but outside a method, constructor or a evaluation of a logical expression exits in between
block. There would only be one copy of each class before complete evaluation, then it is known
variable per class, regardless of how many objects as Short-circuit. ... If there is an expression with
are created from it. &&(logical AND), and first operand itself is false,
4. (b) Keywords then short circuit occurs, the further expression is
Explanation: Java keywords are also known as not evaluated and false is returned.
reserved words. Keywords are particular words 26. (b) Short Circuit OR operator
that act as a key to a code. 27. (a) B
 y skipping the second expression or operand
5. (c) Identifier if possible and save time.
Explanation: Identifiers in Java are symbolic 28. (d) All of the above
names used for identification. They can be a class 29. (d) All of the above
name, variable name, method name, package 30. (c) 9
name, constant name, and more.
31. (d) Compiler error
6. (d) None of the Above
32. (c) %
7. (c) All of the above
33. (b) Left to Right
8. (c) _5thTeam 34. (a) P
 ostfix operators have more priority than
9. (d) All of the Above Prefix operators
10. (d) All of Above 35. (b) P
 refix Increment has less priority than Postfix
11. (d) A and B Decrement
36. (b) Right to Left 52. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
37. (d) All of the above 53. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
38. (a) (++, --) group has higher priority than (+, -, *, /,
%) group 54. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
39. (a) ()
Programming based questions
Fill in the blanks
55. (c) 3
40. (c) Boolean
56. (a) (i) The reverse of the given number is: 456789
41. (d) 8
42. (a) 1 -> 2 -> 3 ==== (b) (ii) ReverseNumber
43. (c) 
Division operator, /, has higher precedence 57. (a) (ii) Compilation error
than multiplication operator
Explanation: Constructors can be chained and
44. (c) Boolean
overloaded. When Test() is called, it creates
45. (d) 1, 2 & 3
46. (d) && another Test object calling the constructor Test(int
47. (a) () temp).
48. (c) Boolean (b) (ii) Sum of natural Number 2
49. (d) 8
58. (a) (i) The number is positive.
Match the following (b) (ii) Check whether the number is positive or

50. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) negative

51. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)

You might also like