Quiz3_92932.pdf

March 09, 2014
Java Quiz 3
Sharif University of Technology
Name :
Student Id :
1. Why is this code allowed in Java, but not in C++?
double array[n]
2. What is the output of the following piece of code?
public class Test{
public static void main(String[] args) {
int a = 1;
torture1(a);
int[] b = new int[1];
b[0] = 1;
torture2(b);
Integer c = new Integer(1);
torture3(c);
System.out.println(a + " " + b[0] + " "+ c);
String s = "hi!";
s.substring(1,2);
System.out.println(s);
}
public static void torture1(int i) {
i = 2; }
public static void torture2(int[] i) {
i[0] = 2; }
public static void torture3(Integer i) {
i = 2;
}}
1
3. Where are each of the mentioned variables in the following piece of code stored?
class Student
{
int stdNo;
}
Student student = new Student();
int a = 2;
Integer b = new Integer(2);
int[] c = new int[2];
student: stack/heap
student.stdNo: stack/heap
a: stack/heap
b: stack/heap
c[0]: stack/heap
c: stack/heap
4. (Bonus Question) What is the output of the following piece of code?
public class Confusing{
private Confusing(Object o){
System.out.println("Object ");
}
Confusing(double [] dArray){
System.out.println("double array");
}
public static void main(String [] args){
new Confusing(null);
}
}
2