0% found this document useful (0 votes)
10 views29 pages

Java Week 2

The document outlines a series of lectures focused on Java programming, covering topics such as basic syntax, control flow, object-oriented concepts, and user input/output. It includes specific Java code examples and exercises for students to complete, as well as details about a graded assignment. Additionally, it contains questions and answers related to Java programming concepts and code execution outcomes.

Uploaded by

syedasara2506
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

Java Week 2

The document outlines a series of lectures focused on Java programming, covering topics such as basic syntax, control flow, object-oriented concepts, and user input/output. It includes specific Java code examples and exercises for students to complete, as well as details about a graded assignment. Additionally, it contains questions and answers related to Java programming concepts and code execution outcomes.

Uploaded by

syedasara2506
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Lec 1.. Learning Outcomes : Comparing a sample program written in C, JAVA and Python. Detailed description of the sample code in JAVA. Compiling and running JAVA code. Lec 2 Learning Outcomes : Scalar types in Java. Variable declaration and value assignment. Operators, Strings, Arrays, type casting in Java. Lec 3 Learning Outcomes : Control Flow of program and program layout. Conditional Execution: if-else Loops: while , for , do-while Directly iterating over array elements using a special for loop. Lec 4.. Learning Outcomes : Introduction to Class and Objects ¢ Definition of a class e Instantiation of a class / creating objects ¢ Initialization of objects. ¢ Constructors and copy constructors. Lec 5 Learning Outcomes : ¢ Different ways of reading input from user in a JAVA program. e Printing and formatting output. [Link] [Link].*; class Rectangle{ int w; //width inth; //height //LINE-1: write the function setw(int) to initialize w //LINE-2: write the function seth(int) to initialize h //LINE-3: write the function area() to return area of rectangle } public class FClass{ public static void main(String[] args) { Scanner sc= new Scanner([Link]); int w = [Link]([Link]()); int h = [Link]([Link]()); Rectangle r = new Rectangle(); [Link](w); [Link](h); int area = [Link](); [Link](area); 2. Write a program to accept a string input from user and print the characters at even indices. import [Link].*; class FClass { public static void main(String[] args){ Scanner sc = new Scanner([Link]); String s1 = [Link](); evenDisplay(s1); } //Define evenDisplay(String) method here 3..Write a program to find the sum of the following series up to n terms. Vc (V2 se 2 )) ae (12 22242 SP2)) oP cocoon + (ZG 22. ee 2) import [Link].*; public class SeriesSum { public static void main(String[] args) { Scanner sc = new Scanner([Link]); int n = [Link](); //Fill your code her int sum = 0; for(int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { sum = sum + (j *j); } } [Link](sum); } } 4... Complete the definition of the given class by defining appropriate constructors and member functions such that it is in coherence with the given main method and produce the required output. import [Link]; class Employee{ String ename; String eid; String edept; public Employee(){ ename = "guest’; } //Define the required methods public Employee(String name, String id, String dept) { ename = name; eid = id; edept = dept; } public void copyDept(Employee e) { [Link] = [Link]; } public void displayDetails() { [Link]("ename : " + [Link]); [Link](‘eid : " + [Link]); [Link]("edept : "+ [Link]); } } public class FClass { public static void main(String args|]) { Scanner s = new Scanner([Link]); Employee e1 = new Employee(); //Enter name of the employee String name = [Link](); //Enter id of the employee String id = [Link](); //Enter department of the employee String dept = [Link](); Employee e2 = new Employee(name,id,dept); [Link](e2); //Copies the department name of e2 into e1's department name. [Link](); Bac Complete the definition of the given class by defining appropriate constructors and member functions such that it is in coherence with the given main method and produce the required output import [Link].*; class Employee { String eid; String ename; String eprojects|]; //Define all the required methods here public Employee(String id, String name, String[] project) { [Link] = id; [Link] = name; [Link] = project; } // Copy constructor public Employee(Employee e) { [Link] = [Link]; [Link] = [Link]; [Link] = [Link]; } public void display() { [Link](‘id:" + eid); [Link](‘name:" + ename); [Link]("projects:'); eprojects[0] = "P001"; for (int i = 0; i < [Link]; i++) { [Link](eprojects[i] + ":"); } } public void mutator() { [Link] = "Mr "+ [Link]; [Link][0] = null; } } public class FClass { public static void main(String[] args) { Scanner s = new Scanner(System. in); String project] = {"P001 ""BQ02""P003"}; //Enter the id of employee String id = [Link](); //Enter the name of employee String name = [Link](); Employee e1 = new Employee(id,jname,project); Employee e2 = new Employee(e1); //The copy constructor must copy all the data members. [Link](); [Link](); } } Week 2 - Graded Assignment 2 The due date for submitting this assignment has passed. Due on 2026-02-22, 23:59 IST. You may submit any number of times before the due date. The final submission will be considered for grading. You have last submitted on: 2026-02-22, 23:32 IST 7 point What is the value of str2 at the end of execution of the following Java code? public class StringExample { public static void main(String[] args) { String str1, str2; str1 = "welcome to IITM'; str2 = [Link](0, 11) + "java course" ; } } java course welcome to IITM java course welcome to java course Error in code Yes, the answer is correct. Score: 1 Accepted Answers: welcome to java course 7 point What is the output of the following Java code ? public class Print { public static void main(String[] args) { int[] a = (1, 2, 3}; int[] b = {1, 2, 3}; [Link](a == b); } } a eee false true {1, 2, 3} == {1, 2, 3} Yes, the answer is correct. Score: 1 Accepted Answers: false 7 point Consider the Java program below. class FClass{ public static void main(String[] args) { int i1 = 10, i2 = 29; double d; d=i2/i1; [Link](d + ""); d = (double)(i2 / i1); [Link](d + ""); d = (double)i2 / i1; [Link](d); } } What will be the output? [Link] DRO DA9) P2{0) 240) 2) P2{0) 2{0) 72{0) Yes, the answer is correct. Score: 1 Accepted Answers: P2{0) 240) ZY) 7 point Identify the correct definition(s) of a boolean variable named flag in Java from the following. boolean flag = 1; boolean flag = true; boolean flag = TRUE; boolean flag = "false"; Yes, the answer is correct. Score: 1 Accepted Answers: boolean flag = true; 7 point Consider the Java program given below. class Point{ private int x; private int y; public Point(int x, int z) { XG Xe Vie Zs } public void printPoint() { [Link]("("+x+")"+y+ DD: } } class CClass{ public static void main(String[] args) { Point p = new Point(10, 20); [Link](); } What will be the output/error? (0, 0) (10, 0) (0, 20) (, ) Compiler error: Invalid assignment in constructor Yes, the answer is correct. Score: 1 Accepted Answers: (0, 20) 7 point Consider the Java program below. import [Link].*; class Example{ public static int doSomething(int num) { intn =num; int total = 0; for(int i = 1; i <= n; i++) { if (n % i == 0) { total = total + i; } } return total; } public static void main(String[] args) { Scanner sc = new Scanner([Link]); int number = [Link](); int x = doSomething(number); } What will x represent? Number of multiples of a number Sum of multiples of a number Number of factors of a number Sum of factors of a number Compiler Error Yes, the answer is correct. Score: 1 Accepted Answers: Sum of factors of a number 7 point Consider the Java code given below. class WhileEx2 { public static void main(String] args) { int i=0; while(i>0) { [Link](i); } do i [Link](i); }while(i>0); } Choose the correct option regarding the given code. Generates no output but program compiles successfully. Generates a compilation error Generates output : 0 0 Generates output : 0 Yes, the answer is correct. Score: 1 Accepted Answers: Generates output : 0 7 point Consider the Java code given below. class Sample 7 public static void main (String args|]) { [Link](10+20+"lIT Madras"); [Link](“IIT Madras"+10+20); } } What will be the output/error? Compilation fails due to [Link](10+20+"lIT Madras"); Compilation fails due to [Link]("IIT Madras"+10+20); 30IIT Madras IIT Madras1020 Exception at run time. Yes, the answer is correct. Score: 1 Accepted Answers: 30IIT Madras IIT Madras1020 7 point Match the following. A. System |. Prints arguments in a new line. B out Il. Public class. C. printIn() Ill. Stream object. D. braces {...} IV. Delimits blocks and statement. A-lll, B-Il, C-IV, D-I A-ll, B-I, C-III, D-IV A-lIl, B-Ill, C-I, D-IV A-I, B-IV, C-ll, D-III Yes, the answer is correct. Score: 1 Accepted Answers: A-Il, B-Ill, C-I, D-IV 7 point How is it possible to run the main method in Java without creating an object? Java is an object oriented programming language. Thus we need to create an object of the main class, and call our main() method to run. Hence the question is fallacious. The modifier static helps the main() method to run independently without creating an object. The access modifier public helps the main() method to run independently without creating an object. The main() method is an exception. It is the only method that exist independently without the dynamic creation of an object. Yes, the answer is correct. Score: 1 Accepted Answers: The modifier static helps the main() method to run independently without creating an object. 7 point Consider the Java code given below. class FClass { public static void main(String args[]) { int arr[] = {0,, 1, 2, 3, 4, 5, 6, 7, 8, 9}; intn = 9; n= arrfarr[n] / 2]; [Link](arr[n] / 3); } Choose the correct option regarding the given code. The program generates output: 0 The program generates output: 1 The program generates output: 1.3333 The program generates a compilation error. Yes, the answer is correct. Score: 1 Accepted Answers: The program generates output: 1 Consider the Java code given below for question 12 and 13. 7 point public class FClass { public static void main(String args[]) { int x} x =1; if(x) { [Link](x); else { [Link]("2"); } } } Choose the correct option regarding the given code. The program generates output: 0 The program generates output: 1 The program generates output: 2 This program will generate an error. Yes, the answer is correct. Score: 1 Accepted Answers: This program will generate an error. 7 point public class FClass { public static void main(String args|]) { int x=1; if(false) { [Link](x); } else { [Link]('True’); } } } The program generates output: True The program generates output: False The program generates output: 1 This program will generate an error. No, the answer is incorrect. Score: 0 Accepted Answers: The program generates output: True 2 points Consider the Java code given below.. public class FClass { public static void main(String args|]) { switch(2) { case 1: [Link]("One’); case 2: [Link]("Two’); case 3: [Link]("Three’); default: [Link]("Default’); break; } } } What will be the output? One Two Default Three None of them Yes, the answer is correct. Score: 2 Accepted Answers: None of them 2 points Match the following. A. Multiple constructors |. constructor with empty arguments B Default constructor ll. Refers to the current class object C. Copy constructors [Link] a new object from an existing one D. this IV. Overloading A-IV, B-I, C-IIl, D-II A-Ill, B-I, C-I, D-II A-IV, B-Il, C-Ill, D-I A-lV, B-III, C-I, D-II Yes, the answer is correct. Score: 2 Accepted Answers: A-lV, B-I, C-III, D-II 2 points Considering the following Java code, choose the correct statement from among the given options regarding this code. class Employee { int eid; String ename; public void display() { [Link](“eid: "+eid); [Link](“ename: "+ename); } } class FClass { public static void main(String[] args) { Employee e1 = new Employee(); [Link](); } } The code results in a compilation error as constructor is not defined. Program runs successfully, and the data members eid and ename contains random garbage value. Program runs successfully, and the data members eid and ename contains 0 and nu //respectively. None of the above options are correct. Yes, the answer is correct. Score: 2 Accepted Answers: Program runs successfully, and the data members eid and ename contains 0 and nu //respectively. 7 point Consider the Java code given below: class Book { private String title = "Unknown Title"; private String author; public Book() { author = "Unknown Author’; } public Book(String t, String a) { title =t author = a; } public void setTitle(String t) { title = t; } public void setAuthor(String a) { author = a; } public String toString() { retur n'"Title:" + title +", Author: "+ author; } } public class TestBook { public static void main(String[] args) { Book b1 = new Book(); Book b2 = new Book("Java Programming", "Riya’); [Link]("Diya’); [Link](b1); [Link](b2); } } What will the output be? Title: Unknown Title, Author: Riya Title: Java Programming, Author: Riya Title: Unknown Title, Author: Unknown Author Title: Java Programming, Author: Riya Title: Unknown Title, Author: Diya Title: Java Programming, Author: Riya Title: Unknown Title, Author: Unknown Author Title: Java Programming, Author: Diya No, the answer is incorrect. Score: 0 Accepted Answers: Title: Unknown Title, Author: Diya Title: Java Programming, Author: Riya 7 point Consider the Java code given below. class Vehicle { private String model; private int year; public Vehicle(String m, int y) { model = m; year =y; } public String toString() { return "model = "+ model +", year =" + year; } } class ElectricVehicle extends Vehicle { private int batteryCapacity; coe CODE BLOCK --------- public String toString() { return [Link]() +", batteryCapacity = "+ batteryCapacity +" kWh"; } } Choose the correct option to fill in place of CODE BLOCK to instantiate instance variables of class ElectricVehicle. public ElectricVehicle(int capacity) { batteryCapacity = capacity; } public ElectricVehicle(String m, int y, int capacity) { model = m; Veahays batteryCapacity = capacity; } public ElectricVehicle(String m, int y, int capacity) { super(m, y); batteryCapacity = capacity; } public ElectricVehicle(String m, int y, int capacity) { [Link] = m; [Link] = y; [Link] = capacity; } Yes, the answer is correct. Score: 1 Accepted Answers: public ElectricVehicle(String m, int y, int capacity) { super(m, y); batteryCapacity = capacity; }

You might also like