JAVA Week 2 GA
JAVA Week 2 GA
java course
welcome to IITM java course
√
welcome to java course
Error in code
Page 2
2. What is the output of the following Java code ? [MCQ:2points]
a == b
√
false
true
{1, 2, 3} == {1, 2, 3}
Solution: It prints false. The “==” operator compares whether a and b are referring
to the same memory location.
Page 3
3. Consider the Java program below. [MCQ:2 points]
class FClass{
public static void main(String[] args) {
int i1 = 10, i2 = 29;
double d;
d = i2 / i1;
System.out.print(d + " ");
d = (double)(i2 / i1);
System.out.print(d + " ");
d = (double)i2 / i1;
System.out.print(d);
}
}
Page 4
4. Identify the correct definition(s) of a boolean variable named flag in Java from the
following. [MSQ:2 points]
boolean flag = 1;
√
boolean flag = true;
boolean flag = TRUE;
boolean flag = "false";
Page 5
5. Consider the Java program below. [MCQ:2 points]
class Point{
private int x;
private int y;
public Point(int x, int z) {
x = x;
y = z;
}
public void printPint() {
System.out.println("(" + x + ", " + y + ")");
}
}
class CClass{
public static void main(String[] args) {
Point p = new Point(10, 20);
p.printPint();
}
}
Solution: In the constructor public Point(int x, int z), for the statements:
x = x;
y = z;
Here the name of function parameter and data member is same for x so inside the
constructor x refers to the local function parameter and not the object’s data member.
Such is not the case with the other parameter z and hence the initialization for y
works in usual manner.
Page 6
6. Consider the Java program below. [MCQ:2 points]
import java.util.*;
class Example{
public static int doSomething(int num) {
int n = num;
int total = 0;
for(int i = 1; i <= n; i++) {
if (n % i == 0) {
total = total + i;
}
}
return total;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int x = doSomething(number);
}
}
What will x represent?
Number of multiples of number
Sum of multiples of number
Number of factors of number
√
Sum of factors of number
Compiler Error
Solution:
Page 7
7. Consider the Java code given below. [ MCQ : 2 points]
class WhileEx2
{
public static void main(String[] args)
{
int i=0;
while(i>0)
{
System.out.println(i);
}
do
{
System.out.println(i);
}while(i>0);
}
}
Solution: In while loop, the body of the loop executes when condition is true,
whereas in do-while loop, first time the loop-body always executes once and at the
end of the loop condition would be checked.
In above code, while loop prints nothing since the condition is failed at the first
time, but do-while prints 0 as the condition is checked at the end of the loop.
Page 8
8. Consider the Java code given below. [ MCQ : 2 points]
class Sample
{
public static void main (String args[])
{
System.out.println(10+20+"IIT Madras");
System.out.println("IIT Madras"+10+20);
}
}
Page 9
9. Match the following. [MCQ:2 points]
Solution:
System: A Java program is a collection of classes. System is a public class in Java.
out: It is a stream object defined in the System class.
println(): Prints arguments in new line like python print()
braces {...} : Delimits blocks and statement. This is similar to the indentation in
Python programming.
Page 10
10. How is it possible to run the main method in Java without creating an object? [MCQ:2
Points]
Java is an object oriented programming language. Thus we need to create
an object of the main class, and call our main() method to run. Hence the
question is fallacious.
√
The modifier static helps the main() method to run independently
without creating an object.
The access modifier public helps the main() method to run independently
without creating an object.
The main() method is an exception. It is the only method that exist indepen-
dently without the dynamic creation of an object.
Solution:
The main() method in java is declared static. Static methods are the methods in
Java that can be called without creating an object of class.
Page 11
11. Consider the Java code given below. [MCQ:2 points]
class FClass
{
public static void main(String args[])
{
int arr[] = {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 9;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 3);
}
}
Solution:
We have int n=9;
arr[arr[9]/2] = arr[9/2] = arr[4] = 4.
Hence, arr[4]/3 = 4/3 = 1.
Page 12
12. Consider the Java code given below. [MCQ:2 points]
(a) public class FClass
{
public static void main(String args[])
{
int x; x = 1;
if(x) { System.out.println(x); }
else { System.out.println("2"); }
}
}
Choose the correct option regarding the given code.
The program generates output:
0
The program generates output:
1
The program generates output:
2
√
This program will generate an error.
(b) public class FClass
{
public static void main(String args[])
{
int x=1;
if(false) { System.out.println(x); }
else { System.out.println("True"); }
}
}
√
The program generates output:
True
The program generates output:
False
The program generates output:
1
This program will generate an error.
Solution:
(a) if is a conditional statement. It accepts boolean as a parameter. Here x is an
integer so this program will yield an error.
(b) If the condition is false, then the else block gets executed.
Page 13
13. Consider the Java code given below..
[MCQ:2 points]
Two
Three
Default
Page 14
14. Match the following. [MCQ:2 points]
Solution:
Multiple constructors — Constructor overloading
Default constructor- If no constructor is defined, compiler creates its own. They do
no have any arguments
Copy constructor — make a copy of an existing object
this — refers to the current class object
Page 15
15. Considering the following Java code, choose the correct statement from among the given
options regarding this code.
[MCQ:2points]
class Employee
{
int eid;
String ename;
class FClass
{
public static void main(String[] args)
{
Employee e1 = new Employee();
e1.display();
}
}
Solution:
If no constructor is defined in a class the default constructor would be executed when
an object is created. In JAVA uninitialized variables have values 0 for numeric types
and null for string types.
Page 16