Sheet-5-oop.docx - Name Marvy Ayman Sec:7 no:1 Sheet 5 Objects and Classes 1 Analyze the following code public class Test public static void main(String

Sheet-5-oop.docx - Name Marvy Ayman Sec:7 no:1 Sheet 5...

This preview shows page 1 - 4 out of 8 pages.

Name: Marvy Ayman Sec:7 no.:1 Sheet 5: Objects and Classes 1) Analyze the following code: public class Test { public static void main(String[] args) { Test nc = new Test(); nc.t = nc.t++; } int t; Test() { } } a. The program has a compilation error because t is not initialized.b. The program does not compile because the parameter list of the mainmethod is wrong.c. The program compiles, but has a runtime error because t has no initialvalue.d.The program has a compilation error because you attempt to create anobject of the Test inside the Test class.e.The program compiles and runs fine. In the following code, suppose that f is an instance of Foo . Answer Questions 2–3. class Foo { int i; static int s; void imethod() { } static void smethod() { } } 2) Which of the following statements is incorrect?
Image of page 1

Subscribe to view the full document.

3) Which of the following statements is incorrect? 4) To restrict access of a data member or a method to the class itself: 5) Analyze the following code: class Test { public static void main(String[] args) { double radius = 5; final static double PI = 3.15169; double area = radius * radius * PI; System.out.println("Area is " + area); } } a.The program has syntax errors because the variable radius is notinitialized.b.The program has syntax errors because a static PI is defined inside amethod.c.The program has no syntax errors but will get a runtime error becauseradius is not initialized.d.The program compiles and runs fine. 6) What are the differences between constructors and methods?
Image of page 2
Answer: constructor method Constructor are special type of methods used to initialize objects of its class . Methods are a set of instructions that are invoked at any point in the program to return a result.
Image of page 3

Subscribe to view the full document.

Image of page 4
  • Fall '17
  • Dr.hatem

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes