Question 2: Answer the following questions: 1. What are two ways to include comments? state when we use each of them?

Similar documents
Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

AP Computer Science Java Mr. Clausen Program 9A, 9B

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Introduction to Java

Using Files as Input/Output in Java 5.0 Applications

Object-Oriented Programming in Java

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Part I. Multiple Choice Questions (2 points each):

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

J a v a Quiz (Unit 3, Test 0 Practice)

Introduction to Programming

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

JAVA ARRAY EXAMPLE PDF

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Java CPD (I) Frans Coenen Department of Computer Science

AP Computer Science Java Subset

Sample CSE8A midterm Multiple Choice (circle one)

AP Computer Science Static Methods, Strings, User Input

Java Programming Language

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Arrays in Java. Working with Arrays

Basics of Java Programming Input and the Scanner class

Chapter 2 Introduction to Java programming

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Comp 248 Introduction to Programming

CS 111 Classes I 1. Software Organization View to this point:

Install Java Development Kit (JDK) 1.8

Java Interview Questions and Answers

Homework/Program #5 Solutions

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Building Java Programs

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Programming Fundamentals I CS 110, Central Washington University. November 2015

Introduction to Java. CS 3: Computer Programming in Java

CS170 Lab 11 Abstract Data Types & Objects

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Building Java Programs

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

The Interface Concept

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Visit us at

Lecture J - Exceptions

LAB4 Making Classes and Objects

Licence Informatique Année Exceptions

In this Chapter you ll learn:

History OOP languages Year Language 1967 Simula Smalltalk

CSE 8B Midterm Fall 2015

Chapter 3. Input and output. 3.1 The System class

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Java: overview by example

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

Some Scanner Class Methods

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Lab 5: Bank Account. Defining objects & classes

D06 PROGRAMMING with JAVA

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Programming Languages CIS 443

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Using Two-Dimensional Arrays

Chapter 1 Java Program Design and Development

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

The following program is aiming to extract from a simple text file an analysis of the content such as:

Topic 11 Scanner object, conditional execution

Introduction to Object-Oriented Programming

(Eng. Hayam Reda Seireg) Sheet Java

Graduate Assessment Test (Sample)

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Teach Yourself Java in 21 Minutes

More on Objects and Classes

Chapter 2 Elementary Programming

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

CS 1302 Ch 19, Binary I/O

6.1. Example: A Tip Calculator 6-1

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Contents. 9-1 Copyright (c) N. Afshartous

WRITING DATA TO A BINARY FILE

public static void main(string[] args) { System.out.println("hello, world"); } }

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Programmierpraktikum

Transcription:

Question 2: Answer the following questions: (30 marks) 1. What are two ways to include comments? state when we use each of them? a. block comments /* */ : is used for multiple lines comment b. line comments,inline comments // : is used for single line comment 2. Write down two main differences between setter and getter methods? Setter Getter 1).usually don t have a return value 1) Must have a return value. 2) have an effect on the state of the object 2) cannot alter the state of the object. 3. What happens if the code throws an exception that is not handled in any try catch statement within the method? Exception propagation 4. Define classes name clash in Java? Suggest one solution for this problem? Name Clash: two classes with the same name in different packages. Solution: If you need two classes from different packages with the same name you have to use the fully qualified class name.

Question 3: write the suitable java statement or code segment from (a-d) questions (10 marks) a. Declare an array named arr of type double with 7 elements. double arr[]=new double [7]; b. Fill the array arr, use the Scanner class to read the input from the keyboard. Scanner input=new Scanner(System.in); for(int i=0;i<arr.length ; i++) arr[i]= input.nextdouble; c.display the number of even elements in the array arr. int counter=0; for(int i=0;i<arr.length ; i++) if(arr[i]%2 == 0) counter++; System.out,println( The number of even elements is +counter); d. Write the suitable import statement to be able to use the Scanner class? Import java.util.scanner;

Question 4: 1. find the output from the following java code (5 marks) : public static void main(string[] args) throws ArithmeticException int x=6; int y=3; double z; for(int i=1;i<=4;i++) try z=x/y; System.out.println("the result is"+z); y--; Output: the result is2.0 the result is3.0 the result is6.0 Division by zero catch (ArithmeticException e) System.out.println("Division by zero"); catch(exception e) 2. Answer the questions related to the following: (8 marks) System.out.println("Error in Division");

2. Use the given Sclass definition and a Test program for it to answer the questions a&b.( marks) public class Test public static void main(stirng args[]) Sclass s1=new Sclass(); s1.inc(); s1.printz(); s1.printw(); Sclass.inc(); s1.printz(); Sclass s2=new Sclass(); s2.inc(); s2.printw(); s1.printz(); s2.printz(); s2.printw(); public class Sclass private static int z; private int w; public static void printz() System.out.println("the value of z "+z); public void printw() System.out.println("the value of w "+w); public static void inc( ) z++; a) Find the output Output the value of z 1 the value of w 0 the value of z 2 the value of w 0 the value of z 3 the value of z 3 the value of w 0 a) If we add the statement System.out.println("the value of w "+w) to the printz() method then the result is : Compilation error, we can t refer a non static variable from a static method

Question5: given below the definition for class Box and class MatchBox, with a test program. Answer the questions form a-j. public class Box a. Declare 3 private variables of type double (width, height and depth) private double width; private double height; private double depth; b. Write a constructor with three parameters to initialize the class variables with them Box(double w, double h, double d) width = w; height = h; depth = d; c. Write a method named getw to return the width public double getw()return width; // a method that will calculate the volume for the Box public double getvolume() return width*height*depth; public String tostring() return the width is +width + \n + the height is +height+ \n + the depth is +depth; public class MatchBox extends Box public double weight; d. Declare the suitable no arguments constructor for the class MatchBox that sets its class variables to the default value. MatchBox( ) super(0,0,0); weight = 0; e. Override the method getvolume in the superclass Box to return the square value for the weight. public double getvolume() return weight*weight; f. Override the tostring method in class Box to print the suitable variables for class MatchBox. public String tostring() return super.tostring()+ the weight is + weight

public static void main(string args[]) g. Declare an object named b1 of type Box Box b1 = new Box(3,5,10); h. Declare an object named mb1 of typematchbox MatchBox mb1 = new MatchBox(); i. Display the volume for object b1. System.out.println(b1.getVolume()); j. Display the object mb1 System.out.println(mb1); Question 6: write a simple java program that reads 10 integers form a file named Data.txt then print the number of occurrences for number 5. public static void main(string[] args) int a; File f= new File("C:\\ Data.txt") ; Scanner input=new Scanner(f); a=input.nextint(); int counter=0; for(int i=1; i<=10;i++) a=input.nextint(); if(a==5) Counter++; System.out.println(counter); input.close();