Java Training in Chennai – Basics – Learning – Part – III
This blog explains about Java Training in Chennai – Basics – Learning – Part – III . It is clearly discussed below .
We have already discussed about some programs in the previous blogs ”Java Training in Chennai – Basics – Learning – Part – I & II ”
______________________________________________________________________
Write a method declaration (just the first line but include the {) for the following description. The wording must be exact and correct!The method must be called calculateFinalExam.
The method must return a double. The method accepts 3 parameters:1. the studentId which must be an integer2. the studentName which must be a String3. the testScore which must be a double.
If we do not provide String array as the argument to the main method as shown below, then the program compiles but throws a run-time error “NoSuchMethodError”. Say True or False
class First
{
public static void main()
{
System.out.println(“Hello Payilagam”);
}
}
If static modifier is removed from the signature of the main method as shown below, then the program compiles but throws a run-time error “NoSuchMethodError”. Say True or False
class Second
{
public void main(String []args)
{
System.out.println(“Hello World”);
}
}
What will be the output of the following program?
public class HaiTwice {
static int num;
public static void main(String[] args) {
HaiTwice p = new HaiTwice();
p.start();
System.out.println(num);
}
void start() {
int var = 7;
twice(var);
System.out.print(var + ” “);
}
void twice(int var) {
var = var * 2;
num = var;
}
}
public class MyObject {
public static void main(String[] args) {
MyObject.myStaticMethod();
System.out.println(MyObject.myStaticMethod());
}
public String memberVar;
static private String memberStaticVar;
static public String myStaticMethod() {
memberVar = “Value”;
memberStaticVar = “Value”;
MyObject obj = new MyObject();
obj.memberVar = “Value”;
System.out.println(“Have a nice to Participants”);
return (“No Error”);
}
}
class MethodCalls
{
public static void main(String[] args)
{
a1(8);
}
static int a1(int a1)
{
System.out.print(” a1 = ” + a1);
return a2(a1++);
}
static int a2(int a2)
{
if (a2 == 0)
{
return 10;
}
System.out.print(” a2 = ” + a2);
return a1(a2 / 2);
}
}
public class PrintStatment10
{
public static void main(String args[])
{
PrintStatment10 obj = new PrintStatment10();
obj.printBinaryFormat(23);
}
public void printBinaryFormat(int number)
{
int binary[] = new int[25];
int index = 0;
while (number > 0)
{
binary[index++] = number % 2;
number = number / 2;
}
for (int i = index – 1; i >= 0; i–)
{
System.out.print(binary[i]);
}
}
}
class A {
public static void main(String arg[]) {
B a = new B();
B b = new B();
B c = new B();
B d = new B();
System.out.println(“i = ” + a.i);
System.out.println(“d = ” + b.d);
System.out.println(“c = ” + c.c);
System.out.println(“s = ” + d.s);
}
}
class B {
int i;
double d;
char c;
String s;
}
class Test
{
public static void main(String args[])
{
for (int x = 0; x < 4; x++)
{
System.out.println(x);
}
System.out.println(x);
}
}
class Test
{
public static void main(String args[])
{
int x;
for (x = 0; x < 4; x++)
{
System.out.println(x);
}
System.out.println(x);
}
}
class Test
{
public static void main(String args[])
{
int a = 5;
for (int a = 0; a < 5; a++)
{
System.out.println(a);
}
}
}
public class LocalPrimitiveVariableInit {
int age;
float salary;
String name;
public void sayHello(){
String message = “This is local variable”;
String reason = ” beacause the variable is in a method”;
System.out.println(“Hello ” + message + reason);
}
public static void main(String[] args) {
String anotherMessage = “This is another local variable in main method”;
int hoursWorked;
System.out.println(“Hours worked : ” + hoursWorked);
LocalPrimitiveVariableInit variableInit = new LocalPrimitiveVariableInit();
System.out.println(“Default Salary is ” + variableInit.salary);
variableInit.sayHello(); // call the method and print message
System.out.println(anotherMessage);
}
}
class San
{
public void m1 (int i,float f)
{
System.out.println(” int float method”);
}
public void m1(float f,int i);
{
System.out.println(“float int method”);
}
public static void main(String[]args)
{
San s=new San();
s.m1(20,20);
}
}
class overload {
int x;
int y;
void add(int a) {
x = a + 1;
}
void add(int a, int b){
x = a + 2;
}
}
class Overload_methods {
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6);
System.out.println(obj.x);
}
}
class overload {
int x;
int y;
void add(int a){
x = a + 1;
}
void add(int a , int b){
x = a + 2;
}
}
class Overload_methods {
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6, 7);
System.out.println(obj.x);
}
}
class overload {
int x;
double y;
void add(int a , int b) {
x = a + b;
}
void add(double c , double d){
y = c + d;
}
overload() {
this.x = 0;
this.y = 0;
}
}
class Overload_methods {
public static void main(String args[])
{
overload obj = new overload();
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + ” ” + obj.y);
}
}
class test {
int a;
int b;
void meth(int i , int j) {
i *= 2;
j /= 2;
}
}
class Output {
public static void main(String args[])
{
test obj = new test();
int a = 10;
int b = 20;
obj.meth(a , b);
System.out.println(a + ” ” + b);
}
}
class MethodOverloading
{
public static void main(String s[])
{
print();
print(8);
print(20 < 10);
}
public static void print()
{
System.out.println(“Called print with no parameters”);
}
public static void print(int i)
{
System.out.println(“Called print with int parameter”);
}
public static void print(boolean b)
{
System.out.println(“Called print with boolean parameter”);
}
}
If the following methods are declared in a class, which of them fail to compile because of overloading rules?
int multiplication(int a, int b) // 1
float multiplication(float a, int b) // 2
float multiplication(int a, float b) // 3
void multiplication(float a) // 4
int multiplication(int a) // 5
void multiplication(int a) // 6
class MethodOverloading
{
public static void main(String s[])
{
int i = 8;
print(i);
print();
int j = 9;
print(j);
}
public static void print()
{
System.out.println(“Called print with no parameters”);
}
public static void print(int i)
{
System.out.println(“Called print with int parameter i”);
}
public static void print(int j)
{
System.out.println(“Called print with int parameter j”);
}
}
class MethodOverloading
{
public static void main(String[] args)
{
int a = 12;
double b = 13;
double c = m(a, b);
double d = m(c, a);
double e = m(a, (int) d);
System.out.println(“c = ” + c + ” d = ” + d + ” e = ” + e);
}
public static double m(int x, double y)
{
return x + y;
}
public static double m(double x, double y)
{
return x – y;
}
public static double m(int x, int y)
{
return x % y;
}
}
class MethodOverloading
{
public static void main(String[] args)
{
int a = 12;
double b = 13;
double c = m(a, b);
double d = m(c, a);
double e = m(a, (int) d);
System.out.println(“c = ” + c + ” d = ” + d + ” e = ” + e);
}
public static double m(int x, double y)
{
return x + y;
}
public static double m(double x, double y)
{
return x – y;
}
public static double m(int x, int y)
{
return x % y;
}
}
_____________________________________________________________
REFERENCES :
https://payilagam.com/blogs/java-training-in-chennai-basics-learning-part-i/
https://payilagam.com/blogs/java-training-in-chennai-basics-learning-part-ii/
COURTESY :
www.proprofs.com, meritcampus.com, www.geeksforgeeks.org, www.f5java.com, dzone.com, www.examveda.com, beingzero.in/, scjptest.combeingzero.in/,
www.gocertify.com, web.cs.iastate.edu, www.sanfoundry.com, https://codingpuzzles.com