Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Sanjivani Rural Education Society’s SANJIVANI K.B.P.POLYTECHNIC, KOPARGAON CLASS-TEST-I Model Answer Sub:- Java Programming YEAR:- 2016-17 Sub. Code:- 17515 Class :- TYCM Div:- A Dept. :- Computer Technology Name of subject teacher:-Prof. Jadhav S.B. Q1. Attempt any THREE (3*3=9) 1. List all the java operators. Answer : ( ½ mark each category) . Categories of operators are as follows: i. Arithmetic operators ii. Logical operators iii. Relational operators iv. Assignment operators v. Conditional operators vi. Increment and decrement operators vii. Bit wise operators 2. What is Byte code? Explain Java Virtual Machine. Answer: ( Bytecode 1 mark, 2 mark) Byte Code: As we know that all programming language compilers convert the source code to machine code. Same job done by Java Compiler to run a Java program, but the difference is that Java compiler convert the source code into intermediate code is called as bytecode. JVM: This machine is called the Java Virtual machine and it exits only inside the computer memory. Following figure shows the process of compilation. Java Program Java Compiler Source Code Virtual Machine Byte Code The Virtual machine code is not machine specific. The machine specific code is generated. By Java interpreter by acting as an intermediary between the virtual machine and real machines shown below Byte Code Virtual machine Java Interpreter Machine code Real Machine 3. What this (?: ) operator is called? Explain with suitable example. Answer : (Correct explanation with example 3 marks) The character pair ?: is a ternary operator of Java, which is used to construct conditional expressions of the following form: Expression1 ? Expression2 : Expression3 The operator ? : works as follows: Expression1 is evaluated if it is true then Expression2 is evaluated and becomes the value of the conditional expression. If Expression1 is false then Expression3 is evaluated and its value becomes the conditional expression. For example: A=3; B=4; C=(A<B)?A:B; C=(3<4)?3:4; C=3 3. State four decision making statements along with their suitable syntax. Answer : 1. if statement 1. Simple if if (condition) { Statement 1; Statement 2; } 2. if..else if (condition) { Statement 1; Statement 2; } else { Statement 3; Statement 4; } nested if- else if (condition1) { if (condition2) { Statement block1; } else { Statement block2; } } else { Statement block3; } Statement 4; 2. switch statement Syntax: switch(condition)// condition means case value { case value-1:statement block1;break; case value2:statement block2;break; case value-3:statement block3;break; … default: statement block-default;break; } statement a; Q2. Attempt any TWO (4*2=8) 1. Write all primitive data types available in java along with their storage sizes in bytes. Answer : (primitive types ½ mark each) In a primitive data types, there are two categories Numeric means Integer, Floating points Non-numeric means Character and Boolean Following table shows the datatypes with their size and ranges. Data type byte boolean char short Int long float double Size (byte) Range 1 -128 to 127 1 True or false 2 A-Z,a-z,0-9,etc. 2 -32768 to 32767 4 (about) -2 million to 2 million 8 (about) -10E18 to 10E18 4 -3.4E38 to 3.4E18 8 -1.7E308 to 1.7E308 Fig: Datatypes with size and range 2. State all features of Java. Explain any two. Answer : (List of features 1 mark, description 1 mark each) Features of Java are as follows: 1. 2. 3. 4. 5. 6. 7. 8. 9. Compiled and Interpreted Platform Independent and portable Object- oriented Robust and secure Distributed Familiar, simple and small Multithreaded and Interactive High performance Dynamic and Extensible 1. Compiled and Interpreted Basically a computer language is either compiled or interpreted. Java comes together both these approach thus making Java a two-stage system. Java compiler translates Java code to Bytecode instructions and Java Interpreter generate machine code that can be directly executed by machine that is running the Java program. 2. Platform Independent and portable Java supports the feature portability. Java programs can be easily moved from one computer system to another and anywhere. Changes and upgrades in operating systems, processors and system resources will not force any alteration in Java programs. This is reason why Java has become a trendy language for programming on Internet which interconnects different kind of systems worldwide. Java certifies portability in two ways. First way is, Java compiler generates the bytecode and that can be executed on any machine. Second way is, size of primitive data types are machine independent. 3. What are the uses of super and this keyword with respect to inheritance? Answer : ( use of super 2 marks, this 2 marks) Use of Super Keyword A subclass constructor used to construct the instance variable of both the subclasses and superclass. The subclass constructor uses the keyword super to invoke the constructor method of the superclass. To use the super keyword following rules must be followed: super is always used within the subclass method constructor method. The call to superclass constructor must appear first statement within the subclass constructor. The parameter used with super call must match the order and type of the instance variable declared in the super class. Use of this keyword When the instance method or constructor being called by an object, then the object is called ‘object under execution’ which is always pointed by ‘this’ pointer. The members of the current object such as constructor or instance variables can be referred by using this operator. 1. this() can be used to invoke current class constructor. 2. this keyword can be used to invoke current class method (implicitly) 3. this can be passed as an argument in the method call. 4. this can be passed as argument in the constructor call. 5. this keyword can also be used to return the current class instance. Q3. Attempt any TWO (4*2=8) 1. Describe different forms of inheritance. Answer : (any four 1 mark each) Different types of inheritance are available: 1. Single Inheritance (Only one super class) 2. Hierarchical Inheritance (One super class many subclasses) 3. Multilevel Inheritance (Derived from a derived class) 4. Hybrid Inheritance (Combination of above three types) Single inheritance When a class extends another one class only then we call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A. “Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes. Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A. 2. What is constructor? Explain constructor overloading with example. Answer : (Constructor description 2 marks, Example program 2 marks) Constructor Objects once created must be given some initial values, this can be done using two methods as stated above one is using dot operator and another is using parameterized method. But it is tedious task to initialize all variables (i.e. object) like this. It is easy and simple to initialize an object when it is created. Java supports a special type of method called constructor. A constructor initializes an object immediately upon creation. It has the same name as the class in which it is defined and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created and before the new operator completes. Constructors have no return type, not even void. This is because the implicit return type of a class’s constructor is the class type itself. 3. Define a class having one 3 digit number, num as data member, initialize and display reverse of that number. (Logic 2-M, Syntax 2-M) class reverse { public static void main(String args[]) { int num = 253; int q,r; for (int I = 0; i<3; i++) { q = num / 10; r = num % 10; System.out.println(r); Num = q; } } } public class PrimeNumCheck { public boolean isPrimeNumber(int number){ for(int i=2; i<=number/2; i++){ if(number % i == 0){ return false; } } return true; } public static void main(String a[]){ PrimeNumCheck mpc = new PrimeNumCheck(); System.out.println("Is 17 prime number? "+mpc.isPrimeNumber(17)); } } **** Best Of Luck ****