Question 1(time 10 Mins): a) Declare an array. Append an integer, a double and a string into it.
b) Print the Array
Question2(time 10 Mins):
Functions:Create a function that will test to see whether a string value contains a valid IPv4 address. An IPv4 address is the address assigned to a computer that uses the internet Protocol (IP) to communicate. An IP address consists of four numeric values that range from 0-255, separated by a dot(period) Example: 10.0.1.250.
Qestion 3(time 10Mins):
Error Handling : Create a class called VendingMachine. It must contain an array of strings called ‘items’ as a stored property e.g - var items: [String] = ["Akshay", "Rahul", "Mayank"]
Declare array and fill data
It must also contain a function ‘getItem’ which takes a string and compares it to the items in the ‘items’ array. If the item is present, it should print ‘Here you go!’ or else it should throw a custom error ‘ItemNotAvailable’.
// Step 2: call let objVendingMachine: VendingMachine = VendingMachine()
// objVendingMachine.getItem(value "Rahul")
// Result should be “Here you go!”
Sol1:
fun main(args: Array<String>) {
val list = mutableListOf<Any>()
list.add(2.0)
list.add(2)
list.add("leetcode")
print(list)
}Sol2:
#include <bits/stdc++.h>
using namespace std;
bool test(string &s) {
if(s[0] == '.' || s[s.length() - 1] == '.') return false;
int dots = 0;
string currNum = "";
for(auto c : s) {
// cout << currNum << " , ";
if(c == '.') {
if(currNum.size() == 0) return false;
int num = stoi(currNum);
currNum = "";
if(num < 0 || num > 255) return false;
dots +=1;
} else {
currNum += string(1, c);
}
}
if(dots != 3) return false;
if(currNum.size() == 0) return false;
int num = stoi(currNum);
if(num < 0 || num > 255) return false;
return true;
}
int main() {
string ipAddress = "1000.0.1.250";
if(test(ipAddress)) cout << "valid ipv4";
else cout << "not valid";
}Sol3:
class VendingMachine {
private val items = mutableListOf<String>("Akshay", "Rahul", "Mayank")
fun getItem(item: String) {
for(it in items) {
if(item.equals(it)) continue;
else throw Exception("custom exception")
}
print("Here you go!")
}
}
fun main() {
val obj = VendingMachine()
obj.getItem("Rahul")
}