Uploaded by shwkt Mohamed Moahmed Elshahd

quiz

advertisement
Fayoum University
Faculty of Engineering
Department of Electrical Engineering
Data structure and algorithm
Quiz No.1 – 30 Mins
1-
second semester 2018/2019
Dr. Sayed Taha
TA/Shawkat Mohamed
What will be the output ?
Public class demo{
private int a, b;
public demo()
{
a = 10;
b = 20;
}
public void print()
{
System.out.println ("a = " + a + " b = " + b + "n");
}
}
Pulic class Test
{
public static void main(String[] args)
{
demo obj1 = new demo();
demo obj2 = obj1;
obj1.a += 1;
obj1.b += 1;
System.out.println ("values of obj1 : ");
obj1.print();
System.out.println ("values of obj2 : ");
obj2.print();
}
}
1values of obj1:
a = 11 b = 21
values of obj2:
a = 11 b = 21
2values of obj1 :
a = 11 b = 21
values of obj2 :
a = 10 b = 20
3values of obj1 :
a = 11 b = 20
values of obj2 :
a = 10 b = 21
2- Public class Test{
int a = 1;
int b = 2;
Test func(Test obj){
Test obj3 = new Test();
obj3 = obj;
obj3.a = obj.a++ + ++obj.b;
obj.b = obj.b;
return obj3;
}
public static void main(String[] args){
Test obj1 = new Test();
Test obj2 = obj1.func(obj1);
System.out.println("obj1.a = " + obj1.a + "
System.out.println("obj2.a = " + obj2.a + "
}}
1obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3
obj1.b = " + obj1.b);
obj1.b = " + obj2.b);
2obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3
3-Which of the following is/are false about constructors in Java?
1) Constructor name should be same as class name.
2) If you don't define a constructor for a class, a default parameterless constructor is automatically created by
the compiler.
3) The default constructor calls super() and initializes all instance variables to default value like 0, null.
4) all of them is true.
4- In java, objects are passed as
1- Copy of that object
2- Method called call by value
3-Memory address
4- Constructor
5- What is the output of the following JAVA program ?
Public Class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
Public void start() {
String stra = ”do”;
String strb = method(stra);
System.out.print(“: ”+stra + strb);
}
String method(String stra) {
stra = stra + ”good”;
System.out.print(stra);
return“ good”;
}}
1-dogood : dogoodgood
2-dogood : gooddogood
3-dogood : dodogood
4-dogood : dogood
Download