Data Types And Variables — Part 2 MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Data Types And Variables — Part 2 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: public class Test { static void test(float x){ System.out.print("float"); } static void test(double x){ System.out.print("double"); } public static void main(String[] args){ test(99.9); } }
Correct Answer: Option B — double
Detailed Academic Explanation:
floating-point numbers are by default of type double. 99.9 is a double not a float. To print "float" cast 99.9 to (float)
💡Key Conceptual Anchor:Data Types And Variables
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following program? public class Test{ static int x = 10 ; public static void main(String[] a){ Test test = new Test( ) ; Test test1 = new Test( ) ; test.x += 1 ; System.out.println( test.x + test1.x ) ; } }
Correct Answer: Option C — 22
Detailed Academic Explanation:
Static variable have a single copy of memory. That means all the objects will share the same memory location. So, if the object test increase the value of x by 1, then object test1 will access that incremented value of x
💡Key Conceptual Anchor:Data Types And Variables
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be output of the following program code? public class Test{ public static void main(String[] a){ short x = 10; x = x*5; System.out.print(x); } }
Correct Answer: Option C — Compilation Error
Detailed Academic Explanation:
lossy conversion from int to short x = x*5; ^ 1 error
💡Key Conceptual Anchor:Data Types And Variables
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: public class Test{ int i = 34; public static void main(String args[]){ Test t1 = new Test(); Test t2 = new Test(); t1.i = 65; System.out.print(t1.i); System.out.print(t2.i); } }
Correct Answer: Option B — 65 34
Detailed Academic Explanation:
Instance variable is unique for their object, that means each copy of memory for variable i will be available for each object. So, changing value of i from one object will not affect the value of i accessed by another object. 1. Two objects of the Test class, t1 and t2 , are created using the new keyword. 2. The default value of the instance variable i is 34 as defined in the class Test . 3. The instance variable i of t1 is then set to 65 using t1.i = 65; 4. When we print t1.i , it will output 65 . 5. When we print t2.i , it will output 34 . This is because t2 is a separate instance of the Test class, and its i remains the default value of 34 . Therefore, the correct output will be 6534 .
💡Key Conceptual Anchor:Data Types And Variables
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The following program: public class Test{ static boolean isOK; public static void main(String args[]){ System.out.print(isOK); } }
Correct Answer: Option B — Prints false
Detailed Academic Explanation:
By default, a boolean variable will contain false.
💡Key Conceptual Anchor:Data Types And Variables
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will the output of the following program? public class Test{ public static void main(String args[]){ float f = (1 / 4) * 10; int i = Math.round(f); System.out.println(i); } }
Correct Answer: Option B — 0
Detailed Academic Explanation:
The result of 1/4 will be zero because integer division is carried out on the operands. If you need to obtain a fractional value you need to use either a float or double literal as in 1F / 4F.
💡Key Conceptual Anchor:Data Types And Variables
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output for the below code? class A{ int k; boolean istrue; static int p; public void printValue(){ System.out.print(k); System.out.print(istrue); System.out.print(p); } } public class Test{ public static void main(String argv[]){ A a = new A(); a.printValue(); } }
Correct Answer: Option A — 0 false 0
Detailed Academic Explanation:
Global and static variable need not be initialized before use. Default value of global and static int variable is zero. Default value of boolean variable is false. Remember local variable must be initialized before use.
💡Key Conceptual Anchor:Data Types And Variables
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output for the below code? public class Test{ int _$; int $7; int do; public static void main(String argv[]){ Test test = new Test(); test.$7=7; test.do=9; System.out.println(test.$7); System.out.println(test.do); System.out.println(test._$); } }
Correct Answer: Option D — Compile error - do is not valid identifier.
Detailed Academic Explanation:
$7 is valid identifier. Identifiers must start with a letter, a currency character ($), or underscore ( _ ). Identifiers cannot start with a number. You can't use a Java keyword as an identifier. do is a Java keyword.
💡Key Conceptual Anchor:Data Types And Variables
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output for the below code? public class Test{ public static void main(String[] args){ int i = 010; int j = 07; System.out.println(i); System.out.println(j); } }
Correct Answer: Option A — 8 7
Detailed Academic Explanation:
By placing a zero in front of the number is an integer in octal form. 010 is in octal form so its value is 8
💡Key Conceptual Anchor:Data Types And Variables
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output for the below code? public class Test{ public static void main(String[] args){ byte b = 6; b+=8; System.out.println(b); b = b+7; System.out.println(b); } }
Correct Answer: Option C — Compilation fails with an error at line 6
Detailed Academic Explanation:
int or smaller expressions always resulting in an int. So compiler complain about Type mismatch: cannot convert from int to byte for b = b+7; But b += 7; // No problem because +=, -=, *=, and /= will all put in an implicit cast. b += 7 is same as b = (byte)b+7 so compiler not complain.
💡Key Conceptual Anchor:Data Types And Variables
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output for the below code? public class Test{ public static void main(String[] args){ byte i = 128; System.out.println(i); } }
Correct Answer: Option C — Compilation fails with an error at line 3
Detailed Academic Explanation:
byte can only hold up to 127. So compiler complain about possible loss of precision. static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 2 7 -1. static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -2 7 .
💡Key Conceptual Anchor:Data Types And Variables
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output for the below code? public class Test{ int i=8; int j=9; public static void main(String[] args){ add(); } public static void add(){ int k = i+j; System.out.println(k); } }
Correct Answer: Option D — Compilation fails with an error at line 8
Detailed Academic Explanation:
i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.
💡Key Conceptual Anchor:Data Types And Variables
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be output of following program? public class Test{ public static void main(String[] args){ byte b=127; b++; b++; System.out.println(b); } }
Correct Answer: Option C — -127
Detailed Academic Explanation:
Range of byte data in java is -128 to 127. But byte data type in java is cyclic in nature.
💡Key Conceptual Anchor:Data Types And Variables
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: public class Test{ int a = 10; public void method(int a){ a += 1; System.out.println(++a); } public static void main(String args[]){ Test t = new Test(); t.method(3); } }
Correct Answer: Option B — 5
Detailed Academic Explanation:
Here's an explanation of the code: 1. The class Test is defined with an instance variable a initialized to 10. 2. The method method is defined within the Test class, which takes an integer parameter a . 3. Inside the method method, the local variable a shadows the instance variable a because they have the same name. 4. The value of the local variable a is incremented by 1 using the += operator, making it 4. 5. The value of the local variable a is then incremented again using the pre-increment operator ++ , making it 5. 6. The value of the local variable a (which is 5) is printed using System.out.println() . 7. In the main method, an instance of the Test class is created. 8. The method method is invoked on the t object with the argument 3. 9. This triggers the execution of the method method, which prints 5 as explained above. Therefore, the output of the code is 5 . Thus, option B is the correct answer.
💡Key Conceptual Anchor:Data Types And Variables
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class increment { public static void main(String args[]) { int g = 3; System.out.print(++g * 8); } }
Correct Answer: Option C — 32
Detailed Academic Explanation:
The given Java code initializes a variable g with the value 3 . It then uses the pre-increment operator ( ++ ) on g before multiplying it by 8 and printing the result using System.out.print . The expression ++g * 8 is evaluated as follows: ++g increments the value of g to 4 . 4 * 8 is equal to 32 . Therefore, the output of the code will be 32 .
💡Key Conceptual Anchor:Data Types And Variables
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class variable_scope { public static void main(String args[]) { int x; x = 5; { int y = 6; System.out.print(x + " " + y); } System.out.println(x + " " + y); } }
Correct Answer: Option D — Compilation error
Detailed Academic Explanation:
The correct answer is D: Compilation error . Here's why: In Java, variables have a scope, which determines where they can be accessed in the code. 'x' is declared outside the inner block, so it's accessible both inside and outside the curly braces {}. 'y' is declared inside the inner block {}. This means 'y' is only accessible within that block. The line 'System.out.println(x + " " + y);' is outside the inner block where 'y' is defined. Therefore, the compiler will give an error because it cannot find a variable named 'y' in that scope. The program will not compile because of the scope issue with the variable 'y'.
💡Key Conceptual Anchor:Data Types And Variables
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The following fraction of code double STATIC = 2.5 ; System.out.println( STATIC );
Correct Answer: Option A — Prints 2.5
Detailed Academic Explanation:
Prints 2.5 is the correct answer to “The following fraction of code double STATIC = 2.5 ; System.out.println( STATIC );”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What would be the output of the following fraction of code? int Integer = 34 ; char String = 'S' ; System.out.print( Integer ) ; System.out.print( String ) ;
Correct Answer: Option E — 34 S
Detailed Academic Explanation:
34 S is the correct answer to “What would be the output of the following fraction of code? int Integer = 34 ; char String = 'S' ; System.out.print( Integer ) ; System.out.print( String ) ;”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, the word true is ................
Correct Answer: Option B — A Boolean literal
Detailed Academic Explanation:
A Boolean literal is the correct answer to “In Java, the word true is ................”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which class does all the Enums extend?
Correct Answer: Option C — Enum
Detailed Academic Explanation:
Enum is the correct answer to “Which class does all the Enums extend”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is the advantage of BigDecimal over double?
Correct Answer: Option D — Precision
Detailed Academic Explanation:
Precision is the correct answer to “Which of the following is the advantage of BigDecimal over double”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code snippet? double a = 0.02; double b = 0.03; double c = b - a; System.out.println(c); BigDecimal _a = new BigDecimal("0.02"); BigDecimal _b = new BigDecimal("0.03"); BigDecimal _c = b.subtract(_a); System.out.println(_c);
Correct Answer: Option A — 0.009999999999999998 0.01
Detailed Academic Explanation:
0.009999999999999998 0.01 is the correct answer to “What will be the output of the following Java code snippet? double a = 0.02; double b = 0.03; double c = b - a; System.out.println(c); BigDecimal _a = new BigDecimal("0.02"); BigDecimal _b = new BigDecimal("0.03"); BigDecimal _c = b.subtract(_a); System.out.println(_c);”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the range of short data type in Java?
Correct Answer: Option B — -32768 to 32767
Detailed Academic Explanation:
-32768 to 32767 is the correct answer to “What is the range of short data type in Java”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Is SimpleDateFormat thread safe?
Correct Answer: Option B — False
Detailed Academic Explanation:
False is the correct answer to “Is SimpleDateFormat thread safe”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these can not be used for a variable name in Java?
Correct Answer: Option B — keyword
Detailed Academic Explanation:
keyword is the correct answer to “Which of these can not be used for a variable name in Java”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Are enums are type-safe?
Correct Answer: Option A — True
Detailed Academic Explanation:
True is the correct answer to “Are enums are type-safe”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the below data type doesn't support overloaded methods for +,-,* and /?
Correct Answer: Option D — BigDecimal
Detailed Academic Explanation:
BigDecimal is the correct answer to “Which of the below data type doesn't support overloaded methods for +,-,* and /”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How to convert a String to a Date object?
Correct Answer: Option A — SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); sdf.parse(new Date())
Detailed Academic Explanation:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); sdf.parse(new Date()) is the correct answer to “How to convert a String to a Date object”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is BigDecimal.ONE?
Correct Answer: Option D — static variable with value 1 on scale 0
Detailed Academic Explanation:
static variable with value 1 on scale 0 is the correct answer to “What is BigDecimal.ONE”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the error in the following Java code? byte b = 50; b = b * 50;
Correct Answer: Option B — * operator has converted b * 50 into int, which can not be converted to byte without casting
Detailed Academic Explanation:
* operator has converted b * 50 into int, which can not be converted to byte without casting is the correct answer to “What will be the error in the following Java code? byte b = 50; b = b * 50;”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these values can a boolean variable contain?
Correct Answer: Option A — True & False
Detailed Academic Explanation:
True & False is the correct answer to “Which of these values can a boolean variable contain”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?
Correct Answer: Option C — double
Detailed Academic Explanation:
double is the correct answer to “If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class dynamic_initialization { public static void main(String args[]) { double a, b; a = 3.0; b = 4.0; double c = Math.sqrt(a * a + b * b); System.out.println(c); } }
Correct Answer: Option A — 5.0
Detailed Academic Explanation:
5.0 is the correct answer to “What will be the output of the following Java program? class dynamic_initialization { public static void main(String args[]) { double a, b; a = 3.0; b = 4.0; double c = Math.sqrt(a * a + b * b); System.out.println(c); } }”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these can be returned by the operator &?
Correct Answer: Option D — Integer or Boolean
Detailed Academic Explanation:
Integer or Boolean is the correct answer to “Which of these can be returned by the operator &”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which data type value is returned by all transcendental math functions?
Correct Answer: Option C — double
Detailed Academic Explanation:
double is the correct answer to “Which data type value is returned by all transcendental math functions”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the prototype of the default constructor of this Java class? public class prototype { }
Correct Answer: Option D — public prototype( )
Detailed Academic Explanation:
public prototype( ) is the correct answer to “What is the prototype of the default constructor of this Java class? public class prototype { }”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program, if we run as "java main_arguments 1 2 3"? class main_arguments { public static void main(String [] args) { String [][] argument = new String[2][2]; int x; argument[0] = args; x = argument[0].length; for (int y = 0; y < x; y++) System.out.print(" " + argument[0][y]); } }
Correct Answer: Option D — 1 2 3
Detailed Academic Explanation:
1 2 3 is the correct answer to “What will be the output of the following Java program, if we run as "java main_arguments 1 2 3"? class main_arguments { public static void main(String [] args) { String [][] argument = new String[2][2]; int x; argument[0] = args; x = argument[0].length; for (int y = 0; y < x; y++) System.out.print(" " + argument[0][y]); } }”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these literals can be contained in float data type variable?
Correct Answer: Option B — -3.4e+038
Detailed Academic Explanation:
-3.4e+038 is the correct answer to “Which of these literals can be contained in float data type variable”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class A { final public int calculate(int a, int b) { return 1; } } class B extends A { public int calculate(int a, int b) { return 2; } } public class output { public static void main(String args[]) { B object = new B(); System.out.print("b is " + b.calculate(0, 1)); } }
Correct Answer: Option C — Compilation Error
Detailed Academic Explanation:
Compilation Error is the correct answer to “What will be the output of the following Java code? class A { final public int calculate(int a, int b) { return 1; } } class B extends A { public int calculate(int a, int b) { return 2; } } public class output { public static void main(String args[]) { B object = new B(); System.out.print("b is " + b.calculate(0, 1)); } }”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code snippet? enum Levels { private TOP, public MEDIUM, protected BOTTOM; }
Correct Answer: Option D — Compilation Error
Detailed Academic Explanation:
Compilation Error is the correct answer to “What will be the output of the following Java code snippet? enum Levels { private TOP, public MEDIUM, protected BOTTOM; }”. This question is standard in competitive examination syllabi for Data Types And Variables.
💡Key Conceptual Anchor:Data Types And Variables
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.