Q: What is the output of the below Java program with two classes?
//Testing1.java
public class Example
{
}
public class Testing1
{
public static void main(String[] args)
{
System.out.println("Hello Boss.!");
}
}
A. Hello Boss.!
B. No Output
C. Compiler error
D. None of the above
Solution: There can not be more than one public class declared inside a single java file.
Q: What is the output of the below Java program?
//bingo.java file
public class Hello
{
public static void main(String[] args)
{
System.out.println("BINGO");
}
}
A. bingo
B. BINGO
C. Compiler error
D. None
Solution: The class name and the java file name should be the same. So, change either file name or class name to match.
Q: State TRUE or FALSE.
A Java class provides encapsulation.
A. TRUE
B. FALSE
C. Compiler error
D. None of the above
Solution: no solution
Q: What is the output of the below java class?
class Fox
{
int legs = 2;
}
class Testing2
{
public static void main(String[] args)
{
Fox t1 = new Fox();
System.out.println("T1 before: " + t1.legs);
t1.legs = 4;
System.out.println("T1 After: " + t1.legs);
}
}
A. T1 before: 4
T1 After: 4
B. T1 before: 2
T1 After: 2
C. T1 before: 2
T1 After: 4
D. Compiler error
Solution: There can be any number of classes in a single .java file.
Q: The value of one primitive variable is assigned to another primitive variable by ___ in Java.
A. Pass by value
B. Pass by reference
C. Compiler error
D. None of the above
Solution: no solution
Q: A primitive variable is passed from one method to another method by ___ in Java.
A. Pass by value
B. Pass by reference
C. Compiler error
D. None of the above
Solution: no solution
Q: An object or primitive value that is passed from one method to another method is called ___ in Java. (Argument / Parameter)
A. Argument
B. Parameter
C. Compiler error
D. None of the above
Solution: no solution
Q: An object or a primitive value that is received in a method from another method is called ___ in Java. (Argument / Parameter)
A. Argument
B. Parameter
C. Compiler error
D. None of the above
Solution: no solution
Q: What is the output of the below Java program that passes an object to another method?
class Food
{
int items;
int show()
{return items;}
}
class Testing9
{
public static void main(String[] args)
{
Food f = new Food();
f.items = 5;
System.out.println("Items Before = " + f.show());
change(f);
System.out.println("Items After = " + f.show());
}
static void change(Food foo)
{ foo.items = 10; }
}
A. Items Before = 10
Items After = 10
B. Items Before = 5
Items After = 5
C. Items Before = 5
Items After = 10
D. Items Before = 10
Items After = 5
Solution: no solution
Q: What is the output of the below Java program that passes primitive values?
class Testing10
{
int rats = 5;
public static void main(String[] args)
{
Testing10 t1 = new Testing10();
System.out.println("Rats Before = " + t1.rats);
modify(t1.rats);
System.out.println("Rats After = " + t1.rats);
}
static void modify(int r)
{ r = 20; }
}
A. Rats Before = 5
Rats After = 5
B. Rats Before = 20
Rats After = 20
C. Rats Before = 5
Rats After = 20
D. Rats Before = 20
Rats After = 5
Solution: The primitive values are passed by value only. So, changes in the method modify does not change the original value.